首页 > 技术文章 > python字符串

yuzhifeng 2017-08-01 09:53 原文

python中字符串的格式
如果定义的变量为name,存储的是字符串类型的值
name(变量) = "xxx"
或者
name(变量) = 'xxx'  双引号或者单引号中的数据称之为字符串
 
 
字符串常见操作
如果有字符串 mystr = 'hello world itcast and itcastcpp',以下是常见的操作
 
find
检测str是否包含再mystr中,如果是则返回开始的索引值,否则返回-1
mystr.find("hello")
输出结果:0   (该数字是要查找的内容在整个字符串中的下标)
 
mystr.find("byby")
输出结果:-1  (因为查找内容不在整个字符串中,所以输出为-1)
 
index
该功能和find相似,都是查找,区别在于如果查找不到内容则抛出异常而不是-1
 
rfind
从字符串右边开始查找内容
 
rindex
从字符串右边开始查找内容,查找不到则抛出异常而不是-1
 
count
返回str在start和end之间 在mystr里面出现的次数
mystr.count("itcast")
输出结果:2  (整个字符串中有两个itcast)
 
replace
把mystr中的str1替换成str2,如果count指定,则替换不超过count次
mystr.replace("it","IT")
输出结果:hello world ITcast and ITcastcpp
mystr.replace("it","IT",1)
输出结果:hello world ITcast and itcastcpp  (替换1个it为大写)
 
split
切割,以str为分隔符切片mystr,如果maxsplit有指定值,则仅分隔maxsplit个字符串
例:
mystr = 'hello world itcast and itcastcpp'
mystr.split(" ")
输出结果:
['hello','world','itcast','and','itcastcpp'] 以空格为分隔符,将逗号替换成空格,并以列表形式存储
mystr.split(" ",2)
['hello','world','itcast and itcastcpp']     以空格为分隔符,将逗号替换成空格,替换两个,并以列表形式存储
 
startswith
检查字符串是否是以obj开头,是则返回True,否则返回False
例:
mystr = 'hello world itcast and itcastcpp'
mystr.startswith("hello")
输出结果:
True
mystr.startswith("Hello")
输出结果:
False
 
endswith
检查字符串是否以obj结束,如果是返回True,否则返回False
例子同上,只是检查字符串结尾
 
lower
转换mystr中所有大写字符为小写
例:
mystr = 'HELLO world itcast and itcastcpp'
mystr.lower()
输出结果:
'hello world itcast and itcastcpp'
 
upper
转换mystr中的小写字母为大写
例:
mystr = 'hello world itcast and itcastcpp'
mystr.upper()
输出结果:
'HELLO WORLD ITCAST ADN ITCASTCCP'
 
ljust
返回一个原字符串左对齐,并使用空格填充至长度width的新字符串
例:
mystr = "hello"
mystr.ljust(10)  数字10是自己设定的字符串长度
'hello     '
 
rjust
返回一个原字符串右对齐,并使用空格填充至长度width的新字符串
例:
mystr = "hello"
mystr.rjust(10)
'     hello'
 
center
返回一个原字符串居中,并使用空格填充至长度width的新字符串
例:
mystr = "hello"
mystr.center(10)
'   hello  '
 
strip
删除mystr字符串两端的空白字符
例:
mystr = "   hello   "
mystr.strip(" ")
'hello'
 
lstrip
删除mystr字符串左边的空白字符
例:
mystr = "   hello   "
mystr.lstrip(" ")
'hello   '
 
rstrip
删除mystr字符串右边的空白字符
例:
mystr = "   hello   "
mystr.rstrip(" ")
'   hello'
 
partition
把mystr以str分割成三部分,str前,str和str后
例:
mystr = 'hello world itcast and itcastcpp'
mystr.partition("itcast")
('hello world','itcast',' and itcastcpp')  以元祖方式存储处理好的数据
 
rpartition
类似于partition()函数,不过是从右边开始
例:
mystr = 'hello world itcast and itcastcpp'
mystr.rpartition("itcast")
('hello world itcast and ',' itcast','cpp')
 
splitlines
按照行分隔,返回一个包含各行作为元素的列表
例:
mystr = "hello\nworld"
print(mystr)
hello
world
mystr.splitlines()
['hello','world']
 
isalpha
判断字符串中是否是纯字母,是为True,否为False
例:
mystr = "hello\nworld"
mystr.isalpha()
False
 
isdigit
判断字符串中是否是纯数字,是为True,否为False
例:
mystr = "hello\nworld"
mystr.isdigit()
False
 
mystr = "123"
mystr.isdigit()
True
 
isalnum
判断字符串中是否都是字母或者数字,是为True,否为False
例:
mystr = "abc"
mystr.isalnum()
True
 
mystr = "123"
mystr.isalnum()
True
 
mystr = "abc123"
mystr.isalnum()
True
 
mystr = "abc  123"  字符串中含有空格,所以不满足字母或者数字的条件
mystr.isalnum()
False
 
isspace
如果mystr中只包含空格,则返回True,否则返回False
例:
mystr = "abc123"
mystr.isspace()
False
 
mystr = ""
mystr.isspace()
False
 
mystr = " "
mystr.isspace()
True
 
mystr = "    "
mystr.isspace()
True
 
join
mystr中每个字符后面插入str,构造出一个新的字符串
例:
str = " "
mystr = ['My','name','is','ZIF']
str.join(mystr)
'My name is ZIF'
 
str = "_"
str.join(mystr)
'My_name_is_ZIF'

推荐阅读