字符串类型1
用途:
姓名、性别、地址等描述性的数据
定义方式:
‘ ‘ , " ", ‘‘‘ ‘‘‘内定义的一串字符
操作:
1.按索引取值(正向取,反向取)顾头不顾尾
msg = ‘hello world‘
print(msg[0])
print(msg[1])
print(msg[-1])
2.切片
print(msg[0:7]) # 输出结果;hello w
print(msg[0:7:2]) # 输出结果;hlow 2表示步长
print(msg[6:1:-1]) # 倒着取值 输出结果:w oll
print(msg[-1::-1]) # 取反,从最后一个开始一直取到最开始的值 输出结果:dlrow olleh
3.长度len
print(msg.__len__())
print(len(msg))
4.成员运算
print(‘llo‘ in msg) # 输出结果:True
5.移除空白strip
password = ‘ keke12345‘
print(password.strip()) # 输出结果:keke12345
password1 = input(">>:").strip()
print(password1)
6.切分split
userinfo = ‘keke@163.com‘
user = userinfo.split(‘@‘)
print(user) # 输出结果:[‘keke‘, ‘163.com‘]
print(user[1]) # 输出结果:163.com
cmd = ‘get /root/a/b/c.txt‘
print(cmd.split()) # 输出结果:[‘get‘, ‘/root/a/b/c.txt‘] 不写分隔符,默认按章空格切分
print(cmd.split(‘/‘)) # 输出结果:[‘get ‘, ‘root‘, ‘a‘, ‘b‘, ‘c.txt‘]
print(cmd.split(‘/‘, 1)) # 输出结果:[‘get ‘, ‘root/a/b/c.txt‘] 只切分一次
print(cmd.rsplit(‘/‘, 1)) # 输出结果:[‘get /root/a/b‘, ‘c.txt‘] rsplit从最后切分
7.循环
username = ‘kekeqiqi‘
for i in username:
print(i)
for j in range(0,10,2):
print(j) # 输出结果0 2 4 6 8
原文:https://www.cnblogs.com/keqing1108/p/13080542.html