字符串是一种序列,可以使用索引,切片,加法,乘法等进行操作。但是字符串是不可变的,不能对它进行赋值操作。
(1)使用单引号来表示字符串,如下:
1 strName='I am bo xiao yuan' 2 print(strName)
(2)使用双引号来表示字符串,如下:
1 strName="I am bo xiao yuan" 2 print(strName)
(3)使用三引号来表示字符串,此种方法可以在多行内显示字符串,三引号中可以使用单引号和双引号,如下:
1 strName='''Hello, 2 I am bo 3 xiao 4 yuan. 5 what's your "name". 6 ''' 7 print(strName)
说明:(1)在Python中,字符串之间可以使用+拼接成新的字符串;(2)字符串变量可以和整数使用*重复拼接相同的字符串;(3)数字和字符串之间不能进行其他运算,否则会报错。
1 first_name = "zhang" 2 last_name = "san" 3 4 num = "13" 5 6 print(first_name + last_name) 7 8 print(first_name * 20) 9 10 print(num + 1) 11 12 ------输出结果----- 13 zhangsan 14 zhangzhangzhangzhangzhangzhangzhangzhangzhangzhangzhangzhangzhangzhangzhangzhangzhangzhangzhangzhang 15 Traceback (most recent call last): 16 File "E:/Python/01_Python基础/bxy_01_hello.py", line 10, in <module> 17 print(num + 1) 18 TypeError: Can't convert 'int' object to str implicitly
字符串的切片
切片方法适用于字符串,列表,元组
1)切片使用索引值来限定范围,从一个大的字符串中切除小的字符串
2)列表和元组都是有序的集合,都能够通过索引值获取对应的数据
字符串[开始索引:结束索引:步长]
指定的区间属于左闭右开型,从起始位开始,到结束为的前一位结束。
从头开始,开始索引数字可以省略,冒号不能省略,到结尾结束,结束索引数字可以省略,冒号不能省略。
步长默认为1,如果连续切片,数字和冒号都可以省略。
# -*- coding:utf-8 -*- a = "0123456789" print(a[:]) # 0123456789 print(a[3:]) # 3456789 print(a[:3]) # 012 print(a[3:5]) # 34 print(a[0:6:2]) # 024 print(a[2::-1]) # 210 print(a[::-1]) # 9876543210 逆序
字符串常见操作
1)find
mystr.find(str, start=0, end=len(mystr)),检查str是否包含在mystr中,如果是则返回开始的索引值,否则返回-1。
# -*- coding:utf-8 -*- mystr = "hello world boxiaoyuan and bodayuan" print(mystr.find("boxiaoyuan")) # 12 print(mystr.find("boxiaoyuan", 0, 10)) # -1
2)index
mystr.index(str, start=0, end=len(mystr)),跟find()方法一样,只不过如果str不在mystr中会报异常。
# -*- coding:utf-8 -*- mystr = "hello world boxiaoyuan and bodayuan" print(mystr.index("boxiaoyuan")) # 12 print(mystr.index("boxiaoyuan", 0, 10)) # -1 12 Traceback (most recent call last): File "E:/Python/01_Python基础/demo05.py", line 5, in <module> print(mystr.index("boxiaoyuan", 0, 10)) # -1 ValueError: substring not found
3)count
mystr.count(str, start=0, end=len(mystr)),返回str在start和end之间在mystr里面出现的次数。
# -*- coding:utf-8 -*- mystr = "hello world boxiaoyuan and bodayuan" print(mystr.count("yuan")) # 2 print(mystr.count("yuan", 0, 10)) # 0
4)replace
mystr.replace(str1, str2, mystr.count(str1)),把mystr 中的str1 替换成str2,如果count 指定,则替换不超过count 次。
# -*- coding:utf-8 -*- mystr = "hello world boxiaoyuan and bodayuan" replaced_str = mystr.replace("bo", "zhang") print(replaced_str) # hello world zhangxiaoyuan and zhangdayuan replaced_str = mystr.replace("bo", "zhang", 1) print(replaced_str) # hello world zhangxiaoyuan and bodayuan
5)split
mystr.split(str=" ", maxsplit),以str为分隔符切片mystr,如果maxsplit有指定值,则仅分隔maxsplit个子字符串。
# -*- coding:utf-8 -*- mystr = "hello world boxiaoyuan and bodayuan" split_str = mystr.split(" ", 2) print(split_str) # ['hello', 'world', 'boxiaoyuan and bodayuan']
6)capitalize
mystr.capitalize(),把字符串的第一个字符大写。
# -*- coding:utf-8 -*- my_str = "hello world boxiaoyuan and bodayuan" cap_str = my_str.capitalize() print(cap_str) # Hello world boxiaoyuan and bodayuan
7)title
my_str.title(),把字符串的每个单词首字母大写。
# -*- coding:utf-8 -*- my_str = "hello world boxiaoyuan and bodayuan" title_str = my_str.title() print(title_str) # Hello World Boxiaoyuan And Bodayuan
8)startswith
mystr.startswith(prefix, start=None, end=None),检查字符串是否是以prefix开头, 是则返回True,否则返回False
# -*- coding:utf-8 -*- my_str = "hello world boxiaoyuan and bodayuan" print(my_str.startswith("hello")) # True print(my_str.startswith("world", 6,20)) # True
9)endswith
mystr.endswith(suffix, start=None, end=None),检查字符串是否是以prefix结尾, 是则返回True,否则返回False
# -*- coding:utf-8 -*- my_str = "hello world boxiaoyuan and bodayuan" print(my_str.endswith("bodayuan")) # True print(my_str.endswith("world", 0, 11)) # True
10)lower
mystr.lower(),转换mystr 中的小写字母为小写
# -*- coding:utf-8 -*- my_str = "hello world boxiaoyuan and bodayuan" title_str = my_str.title() print(title_str) # Hello World Boxiaoyuan And Bodayuan lower_str = title_str.lower() print(lower_str) # hello world boxiaoyuan and bodayuan
11)upper
mystr.upper(),转换mystr 中的小写字母为大写
# -*- coding:utf-8 -*- my_str = "hello world boxiaoyuan and bodayuan" upper_str = my_str.upper() print(upper_str) # HELLO WORLD BOXIAOYUAN AND BODAYUAN
12)ljust
mystr.ljust(width),返回一个原字符串左对齐,并使用空格填充至长度width的新字符串
my_str = "hello" ljust_str = my_str.ljust(10) print(ljust_str) # "hello "
13)rjust
mystr.rjust(width),返回一个原字符串右对齐,并使用空格填充至长度width的新字符串
my_str = "hello" rjust_str = my_str.rjust(10) print(rjust_str) # " hello"
14)center
mystr.center(width),返回一个原字符串居中,并使用空格填充至长度width 的新字符串
my_str = "hello" center_str = my_str.center(10) print(center_str) # " hello "
15)lstrip
mystr.lstrip(),删除mystr 左边的空白字符
my_str = " hello" lstrip_str = my_str.lstrip() print(lstrip_str) # "hello"
16)rstrip
mystr.rstrip(),删除mystr 右边的空白字符
my_str = "hello " rstrip_str = my_str.rstrip() print(rstrip_str) # "hello"
17)strip
mystr.strip(),删除mystr 两边的空白字符
my_str = " hello " strip_str = my_str.strip() print(strip_str) # "hello"
18)rfind
mystr.rfind(str, start=0,end=len(mystr)),类似于find()函数,不过是从右边开始查找
my_str = "hello world boxiaoyuan and bodayuan" print(my_str.rfind("boda")) # 27
19)rindex
mystr.rindex( str, start=0,end=len(mystr)),类似于index(),不过是从右边开始
my_str = "hello world boxiaoyuan and bodayuan" print(my_str.rindex("boda")) # 27 print(my_str.rindex("ceshi")) # 报异常
20)partition
mystr.partition(str),把mystr以str分割成三部分,str前,str和str后
my_str = "hello world boxiaoyuan and bodayuan"
par_str = my_str.partition("bo")
print(par_str) # ('hello world ', 'bo', 'xiaoyuan and bodayuan')
21)rpartition
mystr.rpartition(str),类似于partition()函数,不过是从右边开始
my_str = "hello world boxiaoyuan and bodayuan" par_str = my_str.rpartition("bo") print(par_str) # ('hello world boxiaoyuan and ', 'bo', 'dayuan')
22)splitlines
mystr.splitlines(),按照行分隔,返回一个包含各行作为元素的列表
my_str = """hello world boxiaoyuan and bodayuan """ print(my_str.splitlines()) # ['hello world', 'boxiaoyuan', 'and ', 'bodayuan']
23)isalpha
mystr.isalpha(),如果mystr 所有字符都是字母则返回True,否则返回False
my_str = "abcd" print(my_str.isalpha()) # True my_str = "1234" print(my_str.isalpha()) # False
24)isdigit
mystr.isdigit(),如果mystr 只包含数字则返回True 否则返回False
my_str = "abcd" print(my_str.isdigit()) # False my_str = "1234" print(my_str.isdigit()) # True
25)isalnum
mystr.isalnum(),如果mystr 所有字符都是字母或数字则返回True,否则返回False
my_str = "abcd" print(my_str.isalnum()) # True my_str = "1234" print(my_str.isalnum()) # True my_str = "abcd1234" print(my_str.isalnum()) # True
26)isspace
mystr.isspace(),如果mystr 中只包含空格,则返回True,否则返回False
my_str = "abcd" print(my_str.isspace()) # False my_str = " " print(my_str.isspace()) # True
27)join
mystr.join(str),str中每个字符后面插入mystr,构造出一个新的字符串
name = ["my", "english", "name", "is", "zhangsan"] my_str = " " new_name = my_str.join(name) print(new_name) # my english name is zhangsan