参考 https://www.runoob.com/python/python-strings.html
1.将一个字符串变成列表(每一个字母分开)
str='adwee'
newStr=list(str)
print(newStr)
结果:[‘a’, ‘d’, ‘w’, ‘e’, ‘e’]
-
split方法
str.split(str="", num=string.count(str)).
str – 分隔符,默认为所有的空字符,包括空格、换行(\n)、制表符(\t)等。
num – 分割次数。默认为 -1, 即分隔所有。 -
.将一个字符串变成列表(字母不分开)
str='adwee'
newStr=str.split()
print(newStr)
结果:[‘adwee’]
-去掉字符串中的分隔符并且变成以列表的形式展现出来
str='hi1#hi2#hi3'
print(str.split('#'))
结果:[‘hi1’, ‘hi2’, ‘hi3’]
str='hi1#hi2#hi3#hi4'
print(str.split('#', 2))
结果:[‘hi1’, ‘hi2’, ‘hi3#hi4’]
3.字符串的取操作
- 通过索引获取字符串中字符
str='weather'
print(str[1])
结果:e
- 截取字符串中的一部分
str='adwee'
print(str[1:3])
结果: dw
4.字符串的连接
- 用加号
str='weather'
str2 = 'son'
print(str+' '+str2)
结果:weather son
- 使用join,添加特殊字符
str.join(sequence),sequence – 要连接的元素序列。
str=("a","b","c")
print('-'.join(str))
结果:a-b-c
- 查找一个字符是否包含在字符串中
- 使用in
str='weather'
if "j" in str:
print('YES')
else:
print('NO')
结果:NO
- 使用find
- str.find(str, beg=0, end=len(string))
- str – 指定检索的字符串
beg – 开始索引,默认为0。
end – 结束索引,默认为字符串的长度。
查找到,就返回对应的开始的索引值, 查找不到,就返回 -1
str1="today is beautiful!"
print(str1.find("da"))
print(str1.find("da", 5))
结果:2 -1
- 使用index
如果包含子字符串返回开始的索引值,否则抛出异常。
str1="today is beautiful!"
print(str1.index("da"))
print(str1.index("da", 5))
结果:
2
Traceback (most recent call last):
File “d:/vsCodeWorkespace/test.py”, line 159, in
print(str1.index(“da”, 5))
ValueError: substring not found
6.重复字符串
str='weather'
print(str*3)
结果:weatherweatherweather
7.特殊字符的输出
print('a'+'\n'+'b')
print('a'+R'\n'+'b')
结果:
8. python字符串的格式化
print("My name is %s and weight is %d kg!" % ('hope', 21))
结果:My name is Zara and weight is 21 kg!
9.将字符串原样格式输出
Python 中三引号可以将复杂的字符串进行赋值。
Python 三引号允许一个字符串跨多行,字符串中可以包含换行符、制表符以及其他特殊字符。
三引号的语法是一对连续的单引号或者双引号(通常都是成对的用)
hi='''hi
there'''
print(hi)
结果:
10. unicode字符串
引号前小写的"u"表示这里创建的是一个 Unicode 字符串。如果你想加入一个特殊字符,可以使用 Python 的 Unicode-Escape 编码。
print(u'Hello World!')
print(u'Hello\u0020World!')
结果:
Hello World!
Hello World!
PS:\u0020在unicode编码中是一个空格符
11.replace替换
str.replace(old, new[, max])
old -- 将被替换的子字符串。
new -- 新字符串,用于替换old子字符串。
max -- 可选字符串, 替换不超过 max 次
str1="today is beautiful!"
print(str1.replace("today","Tomorrow", 5))
结果:Tomorrow is beautiful!
12.partition() 方法用来根据指定的分隔符将字符串进行分割。
如果字符串包含指定的分隔符,则返回一个3元的元组,第一个为分隔符左边的子串,第二个为分隔符本身,第三个为分隔符右边的子串。
str1="today is beautiful wow ha ha!"
print(str1.partition("is"))
结果:('today ‘, ‘is’, ’ beautiful wow ha ha!’)
13.去掉指定字符或者空格
str1="ddDdtoday is beautiful!dddddd"
print(str1.strip("d"))
结果:Ddtoday is beautiful!
str2 = " Runoob "; # 去除首尾空格
print str2.strip();
结果:Runoob
14.format
基本语法是通过 {} 和 : 来代替以前的 % 。
format 函数可以接受不限个参数,位置可以不按顺序。
types_of_people = 10
x= f"There are {types_of_people} types of people."
binary="binary"
do_not="don't"
y= f"Those who know {binary} and those who {do_not}"
print(x)
print(y)
print(f"I said:{x}")
print(f"I also said:'{y}'")
hilarious = False
b="hello"
joke_evaluation = "Isn't that joke so funny?!{} {}"
print(joke_evaluation.format(hilarious,b))
结果:
There are 10 types of people.
Those who know binary and those who don’t
I said:There are 10 types of people.
I also said:‘Those who know binary and those who don’t’
Isn’t that joke so funny?!False hello
不按位置:
"{1} {0} {1}".format("hello", "world") # 设置指定位置
结果:
‘world hello world’