python exercise string

版权声明:本文章为博主原创文章,未经博主允许不得转载,如有问题,欢迎留言交流指正 https://blog.csdn.net/finalkof1983/article/details/88814348
#字符串和整型的转换
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 in enumerate(teststr[0:len(teststr):2]):
    print(i)


#输出
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

猜你喜欢

转载自blog.csdn.net/finalkof1983/article/details/88814348