Python 字符串(我失忆了)
我真的失忆了
以下全是字符串
s = 's' d = "d" q = '''q''' sdq = "d 's' " sdz = ''' "s'd'" q''' print(s, d, sdq, q, sdz, sep='\n')
|
字符串拼接操作
索引
sdq= '我叫史迪奇' print(str1[0], str1[3], str1[-1]) print(str1[10])
|
切片
sdq = '0123456789' print('sdq[0:3]', sdq[0:3]) print('sdq[1:]', sdq[1:]) print('sdq[:2]', sdq[:2]) print('sdq[:]', sdq[:]) print('sdq[-4:-1]', sdq[-4:-1]) print(sdq[1:100])
|
代码格式化
字符串的一些使用方法
sdq = 'Stitch' print('find第一次出现b的索引', sdq.find('h')) print('replace', sdq.replace('i', 'I')) print('upper', sdq.upper()) print('lower', sdq.lower()) print('swapcase 对换', 'sdq'.swapcase()) print('去前后空格', ' sdq '.strip(), sep='') print('S t i t c h '.replace(' ', ''))
print(len('S t i t c h'))
|
作者: 我叫史迪奇
本文来自于:
https://sdq3.link/python-zfc.html博客内容遵循 署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0) 协议