Python字符串在實際中的操作手冊
作者:佚名
Python字符串如何進行相關(guān)環(huán)境的融合與使用。這個問題需要大家進行學(xué)習(xí)相關(guān)的知識。下面我們就詳細的看看相關(guān)的技術(shù)問題。
Python字符串在使用的時候是需要我們進行詳細的學(xué)習(xí)。下面我們就看看在具體的操作中如何才能更好的進行這個字符串的使用方法。在之后的使用中還是需要我們繼續(xù)學(xué)習(xí)。
- #coding:utf-8
- #兩個小函數(shù)
- #一、查找字符在字符串中第一次出現(xiàn)的位置.
- def find(string, char):
- index = 0
- while index < len(string):
- if (string[index] == char):
- return index
- index += 11
- return -111
- #二、查找字符在字符串中的總數(shù)
- def findSum(string, char):
- index = 0
- count = 0
- while index < len(string):
- if (string[index] == char):
- count += 1
- index += 1
- return count
- #使用以上兩個函數(shù)
- print "字符1在字符串1211211234中第一次出現(xiàn)的位置: ", find("1211
211234", "1")- print "字符1在字符串1211211234中出現(xiàn)的次數(shù):", findSum("1211211
234", "1")- import string #引入string庫
- print string.find('www.cctv.com', 'com') #result=9
- print string.find('Good','d') #result = 3
- print string.find('canada', 'a',2,9) #result =3,用法如下:
- #string.find(s, sub[, start[, end]])函數(shù)說明
- #Return the lowest index in s where the substring sub is
found such that sub is- #wholly contained in s[start:end]. Return -1 on failure.
Defaults for start and- #end and interpretation of negative values is the same
as for slices.- print string.lowercase #常量,abcdefghijklmnopqrstuvwxyz
- print string.uppercase #常量,ABCDEFGHIJKLMNOPQRSTUVWXYZ
- print string.digits #常量,012345678937
- def isLower(char): #判斷一個字符是否為小寫
- if (string.find(string.lowercase, char) > -1):
- return 'Good'
- return 'bad'
- print isLower('S')
- def isLowertest(char): #另外一種判斷字符是否為小寫的方法
- return char in string.lowercase46 print isLowertest('a')
以上就是對Python字符串的相關(guān)介紹,希望大家有所收獲。
【編輯推薦】
責(zé)任編輯:張浩
來源:
博客園