您好,欢迎来到伴沃教育。
搜索
您的当前位置:首页笔记1:字符串替换、查找、格式化输出

笔记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

Copyright © 2019- bangwoyixia.com 版权所有 湘ICP备2023022004号-2

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务