美文网首页
笔记1:字符串替换、查找、格式化输出

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

作者: Think4doing | 来源:发表于2016-12-25 23:22 被阅读26次

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

相关文章

  • python3学习笔记(一)

    1、字符串替换输出: 2、字符串格式化输出: 3、字符串切片

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

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

  • 第3天-python基础

    一、格式化输出 格式化输出让我们可以对字符串里的内容进行灵活替换。 字符串之前的小写字母 f 表示这个字符串需要进...

  • python 字符串详解

    python 字符串 介绍字符串相关的:比较,截取,替换,长度,连接,反转,编码,格式化,查找,复制,大小写,分割...

  • Jetbrain系列的一些实用快捷键

    替换大小写 快捷键 ctrl + shift + u 查找替换过程中 可以将捕获组格式化为大写或者小写输出 具体用...

  • python数据结构

    字符串修改: 返回一个新的字符串,不会对原有字符串做修改。 程序世界的修改 查找替换 字符串格式化 format方...

  • console简单整理

    向控制台输出信息四种不同方式: log info warn error 替换 格式化说明符: %s | 字符串替换...

  • 实现模板引擎

    字符串替换 Template(模板) 目录 字符串拼接 string format(字符串格式化) 模板替换 自制...

  • x86基础实验-字符串编辑

    简介 对字符串进行复制、查找、替换、大小写转换、输出等编辑功能。 程序运行结果 代码

  • 简单的宏定义(不定期补充)

    控制台输出 RGB色值 格式化字符串 格式化字符串 输出点、坐标、Rect 格式化数字

网友评论

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

      本文链接:https://www.haomeiwen.com/subject/drbdvttx.html