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

2016-12-25  本文已影响26人  Think4doing

#字符串替换 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

上一篇 下一篇

猜你喜欢

热点阅读