字符串是Python中最常用的数据类型。我们可以使用引号('或")创建字符串。创建字符串很简单,只要给变量分配一个值即可。
字符串的创建
例:
>>>field='TF fimaly'
字符串格式化
字符串格式化操作使用百分号(%)实现
例:
>>>print('hello,%s'%'world') hello,world
字符串的基本方法
1.find()
语法:str.find(str,beg=0,end=len(string))
例:
>>>field='do it now' >>>filed.find('do') 0
2.lower()
语法:str.lower()
例:
>>>num='tfFAMILY'>>>print num.lower() tffamily
3.upper()
语法:str.upper()
例:
>>>filed='tffamily'
>>>print filed.upper()
TFFAMILY
4.replece()
语法:str.replece(old,new[,max])
例:
>>>field='do it now,do right now' >>>print field.replece('do','Just do') Just do it now,Just do right now
5.strip()
语法:str.strip([chars])
例:
>>>a='++hello world++' >>>b=a.strip('+') >>>print(b) hello world
字符串还有许多其他方法,不一一列出来了。