笔记1:字符串替换、查找、格式化输出

#字符串替换 bytearray.replace(old, new[, count])

phone_number = '1386-666-0006'

hiding_number = phone_number.replace(phone_number[:9], '*' * 9)

print(hiding_number)

#字符串查找 bytearray.find(sub[, start[, end]])

# Return the lowest index in the data where the subsequence sub is found

search = '168'

num_a = '1386-168-0006'

num_b = '1681-222-0006'

print(search + ' is at ' + str(num_a.find(search)) + ' to ' + str(num_a.find(search) + len(search)-1) + ' of num_a')

print(search + ' is at ' + str(num_b.find(search)) + ' to ' + str(num_b.find(search) + len(search)-1) + ' of num_b')

print(num_a.find(search))

#字符串格式化输出,format

print('{} a word she can get what she {} for.'.format('with', 'came'))

print('{preposition} a word she can get what she {verb} for'.format(preposition='with', verb='came'))

print('{0} a word she can get what she {1} for'.format('with', 'came'))


输出结果:


*********0006

168 is at 5 to 7 of num_a

168 is at 0 to 2 of num_b

5

with a word she can get what she came for.

with a word she can get what she came for

with a word she can get what she came for

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容