#字符串和整型的转换
teststr = '123'
int(teststr)
teststr = 123
str(teststr)
#字符串的最大\最小值,以ASCII码值排序
teststr1 = 'abc'
teststr2 = 'xyz'
print(min(teststr1))
print(max(teststr2))
#输出
a
z
#字符串长度及切片,len(str)用于获取字符串长度,然后对整个字符串以步进2进行切片,再使用enumerate将输出的字符的下标排列打印
teststr = 'abcxyz'
for i,j in enumerate(teststr[0:len(teststr):2]):
print(i,j)
#输出
0 a
1 c
2 y
#zip方法实例,每次从三个字符串中取一个字符,组成新的元组,再列表形式保存所有元组
teststr1 = 'abc'
teststr2 = 'xyz'
teststr3 = '123'
print(list(zip(teststr1,teststr2,teststr3)))
#输出
[('a', 'x', '1'), ('b', 'y', '2'), ('c', 'z', '3')]
#原始字符串,使用r'str'形式书写,str部分不需要进行转义
teststr = r'f:\pythonfiletest\testfile'
#格式化输出
teststr1 = '隔壁老王'
teststr2 = 30
print("name %s age %d" % ('隔壁老王',30))
print("name %s age %d" % (teststr1,teststr2))
#输出
name 隔壁老王 age 30
name 隔壁老王 age 30
#连接+、重复*、成员in\not in
teststr1 = 'abc'
teststr2 = 'xyz'
achar = 'm'
bchar = 'b'
print(teststr1+teststr2)
print(teststr1*3)
if achar not in teststr1:
print("%s 不在字符串teststr1中" % (achar))
if bchar in teststr1:
print("%s 在字符串teststr1中" % (bchar))
#输出
abcxyz
abcabcabc
m 不在字符串teststr1中
b 在字符串teststr1中