Python 视频课程 (5)-Python 字符串
第一课 字符串的基本操作
- # 字符串: 基本操作 字符串取单个字母
- s1 = "I love python."
- print(s1[7]) # p
- print(s1[11]) # o
- # print(s1[15]) # 超过字符串长度 会报错
- # 利用分片截取字符串的子字符串 取一段区间的字符串
- print(s1[7:13]) # python
- print(s1[7:]) # python.
- print(s1[::2]) # Ilv yhn 取奇数位置的字母
- s2 = "a b c d e f g"
- print(s2[::2]) # abcdefg 取奇数位置的字母
- s3 = "abc"
- print(s3 * 10) # abcabcabcabcabcabcabcabcabcabc
- print("python" in s1) # True
- print("java" in s1) # False
- print("java" not in s1) # True
- # 求程度 求最大值 最小值
- print(len(s1)) # 14
- print(max(s1)) # y
- print(min(s1)) # 一个空格 字符串的大小上已 acall 吗值来判断的
小结:
讲了字符串的取单个单词 字符串的索引取单词 (取一段区间的字符串) 分片截取子字符串 (需要指定开始索引和结束索引) 过滤不需要的值 用 * 来使得字符串想成复制字符串 用函数 in 或者 not in 来判断单词是否在字符串中
下面讲解 python 中的字符串 有哪些高级的用法 很重要
第二课 格式化字符串基础 # 字符串格式化就是把一个或多个值替换另一个字符串的某个标记 通俗一点讲 就是替换字符串的标记
- # 字符串格式化基础
- # 什么叫字符串格式化: 静态和动态两部分
- # Hello Bill Hello 李宁 Hello 张三
- # Hello param_name 模板
- # 字符串格式化就是把一个或多个值替换另一个字符串的某个标记
- # 也就是把 静态不变的变成一个模板, 把动态部分就像一个函数一下, 在要用的时候, 把值再放进去 // 在特定的场景下 最后形成一个完成的问候语句
小结:
- # %: 格式化字符串 %s: 字符串 %f 浮点数 %d 整数 (把格式化的值原样输出, 也可以改变输出的值, 比如下面的例子求 pi 的值保留 2 位)
- # step1: 定义一个模板 %s: 字符串 模板中会有静态部分 和 动态部分
- formatStr = "Hello %s,Today is %s,Are there any activities today?"
- # step2: 准备要替换标记的值 这 2 个值我们可以用元组来指定
- values = ("John", "Wednesday")
- # step3: 替换标记
- print(formatStr % values) # Hello John,Today is Wednesday,Are there any activities today?
- # %f 浮点数 %d 整数
- from math import pi
- formatStr = "PI 是圆周率, PI 的值是 %.2f(保留小数点后 %d 位)"
- values1 = (pi,2)
- print(formatStr % values1) # PI 是圆周率, PI 的值是 3.14(保留小数点后 2 位)
- formatStr1 = "这件事的成功率是 %d%%, 如果有 %s 参与的话, 成功率会提升至 %d%%" # %d%% 后面的 2 个 % 表示 保留 %
- values2 = (56, "John", 70)
- print(formatStr1 % values2) # 这件事的成功率是 56%, 如果有 John 参与的话, 成功率会提升至 70%
- formatStr2 = "这件事的成功率是 %d%%, 如果有 %s 参与的话, 成功率会提升至 %d%%"
- values2 = (56, "John",70,33)
- print(formatStr2 % values2) # values2 定义的元组值多了或者少了, 都会抛出异常
第三课 使用 Template 类格式化字符串 (把格式化的值原样输出)
- # 用 Template 类格式化字符串 无论用什么方法去格式化字符串 都少不了 2 个 一个是标记 一个是用什么方法启格式化字符串
- # 标记 $name $age $price // 这个和我们的 shell 中的 引用变量有点像
- # 用什么方式来格式化字符串: substitute 方法
- from string import Template
- template1 = Template("$lang 是我最喜欢的编程语言,$lang 非常容易学习, 而且功能强大.")
- print(template1.substitute(lang = 'Python')) # 这里 调用 关键字参数 lang
- # Python 是我最喜欢的编程语言, Python 非常容易学习, 而且功能强大.
- template2 = Template("${s}stitute")
- print(template2.substitute(s = "sub"))
- # substitute
- template3 = Template("$dollar$$ 相当于多少 $pounds") # $dollar$$ 后面的就表是 原始的值
- print(template3.substitute(dollar =20, pounds = "英镑"))
- # 20$ 相当于多少英镑
- template4 = Template("$dollar$$ 相当于多少 $pounds")
- data = {
- } # 这是用字典的方式 这个后面详细都讲到 这一部分用的是字典来进行格式化的
- data['dollar'] = 100
- data['pounds'] = '英镑'
- print(template4.substitute(data))
- # 100$ 相当于多少英镑
- from string import Template
- template01 = Template("Hello $name,Today is $date,Are there any activities today?")
- print(template01.substitute(name = "majihui",date = "Wednesday"))
- #Hello majihui,Today is Wednesday,Are there any activities today?
- template02 = Template("PI 是圆周率, 值为 $pi(保留 $num 位小数)")
- print(template02.substitute(pi = pi ,num = 2))
- # PI 是圆周率, 值为 3.141592653589793(保留 2 位小数)
- template03 = Template("这件事的成功率是 $num1, 如果有 $name 参与的话, 成功率会提升至 $num2")
- print(template03.substitute(num1 = '50%' ,name = "majihui", num2 = '60%'))
- # 这件事的成功率是 50%, 如果有 majihui 参与的话, 成功率会提升至 60%
第四课 使用 format 方法格式化字符串 (把格式化的值原样输出)
- # 使用 format 方法格式化字符串 也就是说, 我们用字符串本身的 format 方法去格式化字符串本身
- # 标记 (格式化参数):{
- ...
- }
- # 如何进行格式化:"template".format(...)
- # 按顺序来指定格式化参数值
- s1 = "Today is {}, the temperature is {} degrees."
- print(s1.format("Saturday", 30))
- # Today is Saturday, the temperature is 30 degrees.
- # 使用命名格式化参数
- s2 = "Today is {week}, the temperature is {degree} degrees."
- print(s2.format(week = "Sunday", degree= 21))
- print(s2.format(degree= 21,week = "Sunday"))
- # Today is Sunday, the temperature is 21 degrees.
- # 混合使用顺序格式化参数和命名格式化参数
- s3 = "Today is {week}, {}, the {} templature is {degree} degrees."
- print(s3.format("abcd", 1234, degree = 43, week="Sunday"))
- # print(s3.format("abcd", degree = 43,1234, week="Sunday")) # 抛出异常 前面必须为顺序的传入参数值
- # Today is Sunday, abcd, the 1234 templature is 43 degrees.
- # 使用序号格式化参数 加序号, 从 0 开始
- s4 = "Today is {week}, {1}, the {0} temperature is {degree} degrees."
- print(s4.format("abcd",1234,degree=44,week="Sunday"))
- # Today is Sunday, 1234, the abcd temperature is 44 degrees.
- # 获取列表中的指定值
- fullname = ["Bill", "Gates"]
- s5 = "Mr. {name[1]}"
- print(s5.format(name = fullname)) # Mr. Gates
- #s7 = "Mr. {fullname[0]}" # 我用都这种方法不对 这个是不对的
- #print(s7.format("Mr. {fullname[0]}"))
- import math
- s6 = "The {mod.__name__} module defines the value {mod.pi} for PI" # .__name__ 就是模块的一个方法, 可以直接用
- print(s6.format(mod = math))
- # The math module defines the value 3.141592653589793 for PI
- print("-----------------------")
- s1 = "Hello {},Today is {},Are there any activities today?"
- print(s1.format("majihui","Wednesday"))
- # Hello majihui,Today is Wednesday,Are there any activities today?
- s2 = "PI 是圆周率, 值为 {pi}(保留 2 位小数)"
- print(s2.format(pi = pi))
- # PI 是圆周率, 值为 3.141592653589793(保留 2 位小数)
- s3 = "这件事的成功率是 {}, 如果有 {} 参与的话, 成功率会提升至 {}"
- print(s3.format('50%','majihui','60%'))
- # 这件事的成功率是 50%, 如果有 majihui 参与的话, 成功率会提升至 60%
第五课 更进一步控制字符串格式化参数 字符串格式化类型符 format 方法
- # 更进一步控制字符串格式化参数
- # 字符串格式化类型符
- # repr 函数 repr('a') = 'a' str('a') = a repr 输出的就是 py 的字符串的输出格式
- # 原样输出的 用 !s 调用 repr 函数输出用 !r Unicode 编码输出用 !a
- s1 = "原样输出:{first!s} 调用 repr 函数:{first!r} 输出 Unicode 编码:{first!a}"
- print(s1.format(first = "中"))
- # 输出的结果为 : 原样输出: 中 调用 repr 函数:'中' 输出 Unicode 编码:'\u4e2d'
- # 将一个整数按浮点数格式输出 :f 字符串格式化类型符
- s2 = "整数:{num} 浮点数:{num:f}"
- print(s2.format(num=123))
- # 整数: 123 浮点数: 123.000000
- # 进制转换
- s3 = "十进制:{num} 二进制:{num:b} 八进制:{num:o} 十六进制:{num:x}"
- print(s3.format(num = 78))
- # 十进制: 78 二进制: 1001110 八进制: 116 十六进制: 4e
- # 原有的方式是这样的
- n = 78
- print(bin(n)) # 0b1001110
- print(oct(n)) # 0o116
- print(hex(n)) # 0x4e
- # 将整数按科学计数法
- s4 = "科学计数法:{num:e}"
- print(s4.format(num =6789))
- # 科学计数法: 6.789000e+03
- m = 6789
- print(format(m,'e'))
- # 6.789000e+03
- # 将浮点数按百分比输出
- s5 = "百分比:{num:%}"
- print(s5.format(num = 0.34))
- # 百分比: 34.000000%
小结
'''
a 将字符串按 Unicode 编码输出
b 将一个整数格式化为一个二进制数
c 将一个整数解释成 ASCII
d 将整数格式化为十进制的数
e/E 科学计数法表示
f/F 将一个整数格式化为浮点数,(nan 和 inf) 转换为小写
g/G 会根据整数值的位数, 在浮点数和科学计数法之间切换, 在整数位超过 6 位时, 与 e 相同, 否则与 f 相同
o 将一个整数格式化为八进制
s 按原样格式化字符串
x/X 将一个整数格式化为十六进制数
% 将一个数值格式化为百分比形式
'''
小结:
推荐使用 format 去格式化字符串, 也可以格式化数字, 其实都是可以的, 看个人的选择.
参考链接: 07 python 数字的格式化输出 format(重要) https://blog.51cto.com/12445535/2463368
来源: http://www.bubuko.com/infodetail-3366101.html