python exercise string之一常规操作

#字符串和整型的转换
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中

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值