1.字符串spa
获取字符串的字符,例如:code
test = 'abcd' a= test[0] # 经过索引,下标,获取字符串中的某一个字符 print(a) b = test[0:1] # 经过下标的 范围获取字符串中的一些字符 #0 <= ** <1 print(b) c =len(test) # 获取字符串中有多少字符组成(不是下标) print(c)
运算结果:blog
a a 4 Process finished with exit code 0
2.list列表 用[ ]表示的 ,例如:a = [love,friend,home,"你好"]索引
test = [123,'adss'] print(type(test),test)
运算结果:字符串
<class 'list'> [123, 'adss'] Process finished with exit code 0
3.利用循环获取字符串中的每一个字符it
第一种:class
test = '你好\t谢谢' count = 0 while count < len(test): a=test[count] print(a) count += 1
运算结果:test
你
好
谢
谢
Process finished with exit code 0
第二种:变量
for 变量名 in 字符串:循环
代码块
test = '你好\t谢谢' for a in test: print(a)
运算结果:
你
好
谢
谢
Process finished with exit code 0
注意:字符串一旦建立没法修改,若是要修改,则会建立新的字符串。
4.替换
test = 'abcabcabc' a = test.replace('a','q',1) # 将q替换第一个a print(a)
运算结果:
qbcabcabc
Process finished with exit code 0