免费的html大作业网站,wordpress 统计2次,网站发展阶段怎么做,给设计网站做图使用字符串
使用字符串格式#xff1a;精简版
百分号 %
# 指定要设置其格式的值时#xff0c;可使用单个值#xff08;如字符串或数字#xff09;#xff0c;可使用元组#xff08;如果要设置多个值得格式#xff09;#xff0c;还可使用字典format …使用字符串
使用字符串格式精简版
百分号 %
# 指定要设置其格式的值时可使用单个值如字符串或数字可使用元组如果要设置多个值得格式还可使用字典format Hello, %s. %s enough for ya? values (world, Hot) format % values
Hello, world. Hot enough for ya?类似UNIX shell的语法 from string import Template tmpl Template(Hello, $who! $what enough for ya?) tmpl.substitute(whoMars, whatDusty)
Hello, Mars! Dusty enough for ya?format {}, {} and {}.format(first, second, third)
first, second and third {0}, {1} and {2}.format(first, second, third)
first, second and third命名字段 from math import pi {name} is approximately {value:.2f}..format(valuepi, nameπ)
π is approximately 3.14.如果变量与替换字段同名还可使用一种简写f字符串 from math import e fEulers constant is roughly {e}.
Eulers constant is roughly 2.718281828459045.使用字符串格式完整版
替换字段名
按顺序将字段和参数配对还可给参数指定名称 {foo} {1} {bar} {0}.format(1, 2, bar4, foo3)
3 2 4 1可访问其组成部分 fullname [Alfred, Smoketoomuch] Mr {name[1]}.format(namefullname)
Mr Smoketoomuch可使用据点表示发来访问导入模块总的方法、属性、变量和函数 import math tmpl The {mod.__name__} module defines the value {mod.pi} for π tmpl.format(modmath)
The math module defines the value 3.141592653589793 for π转换标志
s代表strr代表repra代表ascii print({pi!s} {pi!r} {pi!a}.format(piπ))
π π \u03c0字符串格式设置中的类型说明符
类型含义b将整数表示为二进制数c将整数解读为Unicode码点d将整数视为十进制数进行处理这是整数默认使用的说明符e使用科学表示法来表示小数用e来表示指数E与e相同但使用E来表示指数f将小数表示为定点数F与f相同但对于特殊值nan和inf使用大写表示g自动在定点表示法和科学表示法之间做出选择。这是默认用于小数的说明符但在默认情况下至少有1位小数G与g相同但使用大写来表示指数和特殊值n与g相同但插入随区域而异的数字分隔符o将整数表示为八进制数s保持字符串的格式不变这是默认用于字符串的说明符x将整数表示为十六进制数并使用小写字母X与x相同但使用大写字母%将数表示为百分比值乘以100按说明符f设置格式再在后面加上%
宽度、精度和千位分隔符
宽度使用整数指定 {num:10}.format(num3) 3{name:10}.format(nameBob)
Bob 精度也是使用整数指定的 Pi day is {pi:.2f}.format(pipi)
Pi day is 3.14可同时指定宽度和精度 {pi:10.2f}.format(pipi) 3.14使用都好来指出千位分隔符 One googol is {:,}.format(10**100)
One googol is 10,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000同时指定其他格式设置元素时这个逗号应放在宽度和表示精度的句点之间。
符号、对齐和使用0填充
在指定宽度和精度的数前面可添加一个标志 {:010.2f}.format(pi)
0000003.14要指定左对齐、右对齐和居中可分别使用、和^。 print({0:10.2f}\n{0:^10.2f}\n{0:10.2f}.format(pi))
3.14 3.14 3.14还有更具体的说明符它指定将填充字符放在符号和数字之间。 print({0:10.2f}\n{1:10.2f}.format(pi, -pi)) 3.14 -3.14 print({0:10.2f}\n{1:10.2f}.format(pi, -pi)) 3.14
- 3.14正数加符号表示和正数前面加空格表示 print({0:-.2}\n{1:-.2}.format(pi, -pi)) #默认设置
3.1
-3.1 print({0:.2}\n{1:.2}.format(pi, -pi))
3.1
-3.1 print({0: .2}\n{1: .2}.format(pi, -pi)) 3.1
-3.1井号#选项
# 对于二进制、八进制和十六进制转换将加上一个前缀{:b}.format(42)
101010 {:#b}.format(42)
0b101010# 对于各种十进制数它要求必须包含小数点对于类型g它保留小数点后面的零。{:g}.format(42)
42 {:#g}.format(42)
42.0000字符串方法
center 通过在两边添加填充字符默认为空格让字符串居中 ljust、rjust、zfillfind 在字符串中查找子串。如果找到就返回子串的第一个字符的索引否则返回-1。 rfind、index、rindex、count、startswith、endswithjoin 用于合并序列的元素 splitlower 返回字符串的小写版本 islower、istitle、isupper、translate、capitalize、casefold、swapcase、title、upper
# title 词首大写thats all folks.title()
ThatS All, Folks
# 另一种方法是使用模块string中的函数capwords。import string string.capwords(thats all, folks)
Thats All, Folksreplace 替换 translate、expandtabssplit 与join相反用于将字符串拆分为序列 join、partition、rpartition、rsplit、splitlinesstrip 将字符串开头和末尾的空白但不包括中间的空白删除并返回删除后的结果 lstrip、rstriptranslate 进行单字符替换 replace、lower
# str.maketrans 创建转换表table str.maketrans(cs, kz)table
{115: 122, 99: 107}
# 转换this is an incredible test.translate(table)
thiz iz an inkredible tezt
# maketrans 第三个参数指定要将哪些字母删除table str.maketrans(cs, kz, ) this is an incredible test.translate(table)
thizizaninkredibletezt判断字符串是否满足特定的条件如isspace、isdigit和isupper 表示包含的字符全为空白、数字或大写。isalnum、isalpha、isdecimal、isdigit、isidentifier、islower、isnumeric、isprintable、isspace、istitle、isupper。
小结
字符串格式设置求模运算符%可用于将值合并为包含转换标志如%s的字符串这让你能够以众多方式设置值的格式如左对齐或右对齐指定字段宽度和精度添加符号正号或负号以及在左边填充0等。
字符串方法字符串有很多方法有些很有用如split和join有些很少用到如istitle和capitalize。