零基础学python之str怎么用?(二)
1、# 针对字符串:将某个字符串插入到后面字符串所有的字符中间,拼接成一个新字符串。s = '**'.join('good123')print(s)
2、print('***************切片*******************')s = 'good_make_dog_love_pig'c = s[-1] # 根据下标查询对应的字符,如果从左边数,从0开始。如果从右边数,从-1开始。print(c)
3、# s[x:y:z] 从第x个字符开始,查询到第y个字符(不包含y),z是步长,步长默认是1。s = 'good_make_dog_love_pig'res = s[0:10:2]print(res, type(res))
4、s = 'good_make_dog_love_pig'# s[x:y:z] 从第x个字符开始,查询到第y个字符(不包含y),z是步长,步长默认是1。res = s[0:10:2]print(res, type(res))
5、res = s[-1::-1] # 步长为负数,代表从右往左查询print(res, type(res))
6、print('***********编码*************')a = ord('a') # 查询某个字符对的ASCII码print(a, type(a))
7、c = chr(97) # 根据编码查询对应的字符 (ASCII)print(c)c = 'h'print(chr(ord(c)+1))print('给定2个小写字母,a<b,从2个字母中间随机出一个字母')a = 'a'b = 'f'print(chr(random.randrange(ord(a), ord(b)+1)))
声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
阅读量:132
阅读量:139
阅读量:73
阅读量:190
阅读量:168