Python str 模块
str字符串
- str
- 转义字符
- 格式化
- 内建函数
字符串
- 表示文字信息
- 用单引号,双引号,三引号引起来
s = 'i love wangxiaojing'
print(s)
i love wangxiaojing
s ="i ch liebe wangxiaojing "
print(s)
i ch liebe wangxiaojing
s ="""
I
love
Xiaojing
"""
print(s)
I
love
Xiaojing
转义字符
- 用一个特色的方法表示出一系列不方便写出的内容,比如回车键,换行符,退格键
- 借助反斜杠字符,一旦字符串中出现反斜杠,则反斜杠后面一个或者几个字符表示已经不是原来
的意思了,进行了转义 - 在字符串中,一旦出现反斜杠就要加倍小心,可能由转义字符出现
- 不同系统对换行操作有不同表示
- windows: \n
- Linux: \r\n
# 转义字符的案例
# 想表达Let's Go
# 使用转义字符
s = 'Let\'s Go'
print(s)
# 使用单双引号嵌套
s = "Let's Go"
print(s)
# 表示斜杠
# 比如表示C:\User\Augsnao
s = "C:\\user\\Augsnano"
print(s)
# 回车换行
# 想表达的效果是:
# lch
# lieb
# wangixaojing
# windows 下也可以使用\r\n,效果相同
s = "lch\rlieb\nWangxiaojing"
print(s)
Let's Go
Let's Go
C:\user\Augsnano
lch
lieb
Wangxiaojing
常用的转义字符
转义字符 描述
\(在行尾时) 续行符 ★
\\ 反斜杠符号 ★★★
\' 单引号 ★★★★★
\" 双引号 ★★★★★
\a 响铃
\b 退格(Backspace)
\e 转义
\000 空
\n 换行 ★★★★★
\v 纵向制表符
\t 横向制表符 ★★★★★
\r 回车 ★★★★★
\f 换页
\oyy 八进制数,yy代表的字符,例如: \o12代表换行
\xyy 十六进制数,yy代表的字符,例如:\x0a代表换行
\other 其它的字符以普通的格式输出
# 单个斜杠的用法
# 在python里,单个反斜杠表示此行未结束,处于美观,需要下一行继续
# 理论上应该写成def myDemo(int x, int y, int z):
def myDemo(x,\
y,\
z):
print("hahahhaha")
myDemo(1,2,3)
hahahhaha
格式化
- 把字符串按照一定格式进行打印或者填充
- 格式化的分类:
- 传统格式化
- format
# 填充
s = "l love wangxiaojing"
print(s)
s = "l love lixiajing"
print(s)
s = "l love zhangxiaojing"
print(s)
l love wangxiaojing
l love lixiajing
l love zhangxiaojing
字符串的传统格式化方法
使用%进行格式化
%(百分号)也叫占位符
%s: 字符串
%r: 字符串,但是是使用repr而不是str
%c: 整数转换为单个字符
%d: 十进制整数
%u: 无符号整数
%o: 表示八进制
%x: 十六进制,字母为小写(x为小写)
%X: 十六进制,字母为大写X为大写)
%e: 浮点数(E为小写),例如2. 87e+12
%E: 浮点数(E为大写),例如2. 87E+12
%f,%F: 浮点数十进制形式
%g,%G: 十进制形式浮点或者指数浮点自动转换
格式字符前出现整数表示此占位符所占位置的宽度
格式字符前边出现‘-’ 表示左对齐
格式字符前边出现‘+’ 表示右对齐
0位数不足用*0'’ 补齐
width表示宽度
pricision精度
# %s 表示简单的字符串
# 占位符可以单独使用
s = "l love %s"
print(s)
s = "l love %s"
print(s%"王晓静")#运行到%时检测占位
l love %s
l love 王晓静
print("I love %s"%"李晓静")# %是个占位符,前面是一个字符串,说明%占位符后面必须用字符串
# 占位符一般只能被同类型替换,或者替换类型能被转换成占位符的类型
# 特例
print("I love %s"%100)
I love 李晓静
I love 100
# 占位符是一般只能被同类型替换
s = "yege今年 %d 岁了"
print(s%"十九") #占位符为十进制整数:%d,所以报错提示需要十进制整数类型,不需要字符串
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-10-c7bcf148ec87> in <module>()
1 s = "yege今年 %d 岁了"
----> 2 print(s%"十九")
TypeError: %d format: a number is required, not str
s = "I am %f KG weight, %f m Heigh"
print(s)
# 如果需要格式化信息多余一个,则用括号括起来就可以
# 以下打印使用了默认格式,多余打出了好多个零
print(s%(75.3,1.84)) #s中有两个占位符,按顺序替换占位符
#实际需要惊醒格式化的信息的数量必须与百分号后面给出的数据数量匹配,否则报错
# 如下例子,实际需要格式化的为4处,但是给出数据为三个,则报错
s = "I am %.2f KG weight, %.2f m Heigh"#.2保留小数点后两位
print(s%(75.3,1.84))
I am %f KG weight, %f m Heigh
I am 75.300000 KG weight, 1.840000 m Heigh
I am 75.30 KG weight, 1.84 m Heigh
format格式化
- 使用函数形式进行格式化,代替以前的百分号
# 不用指定位置,按顺序读取
# 方式1
s = "{} {}!"
print(s.format("Hello","world"))# format中的参数有无限多个
# 方式2
s = "{} {}!".format("hello","world")
print(s)# 相当于打印”Hello world”
# 设置指定位置
s = "{0} {1}".format("hello","world")
print(s)
# 设置指定位置
s = "{1} {0}".format("hello","world")
print(s)
# 设置指定位置
s = "I love {0} and {0} loves me".format("hello","world")
print(s)
# 下面案例报错,跟上面案列进行对比
#s = "I love {} and {} loves me".format("hello")
#print(s)
Hello world!
hello world!
hello world
world hello
I love hello and hello loves me
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-40-e80e00c2bede> in <module>()
21
22 # 下面案例报错,跟上面案列进行对比
---> 23 s = "I love {} and {} loves me".format("hello")
24 print(s)
IndexError: tuple index out of range
# 使用命令参数
s = "我们的是{school_name}, 我们的网址是{url}, {teacher}最帅"
s = s.format(school_name="中国科技大学",url="www.baidu.com",teacher="野哥")
print(s)
我们的是中国科技大学, 我们的网址是www.baidu.com, 野哥最帅
# 通过字典设置参数,需要解包
#使用命令参数
s = "我们的是{school_name}, 我们的网址是{url}, {teacher}最帅"
s_dict = {"school_name":"中国科技大学",\
"url":"www.baidu.com",\
"teacher":"yege"}
#**是解包操作
s =s.format(**s_dict)
print(s)
我们的是中国科技大学, 我们的网址是www.baidu.com, yege最帅
# 对数字的格式化需要用到
s = "Liu Dana is {:.2f}m heigh, {:.2f}KG weight"
print(s.format(1.84, 76.45))
Liu Dana is 1.84m heigh, 76.45KG weight
^,<,>分别是剧中、左对齐、右对齐,后面带宽度
:号后面带填充的字符,只能是一个字符,不指定则默认是用个空格填充
+ 表示在正数前显示+,负数前显示-, (空格)表示在整数前加空格
d、d、o、x分别是二进制、十进制、八进制、十六进制
此外我们可以使用大括号{}来转义大括号
str内置函数
- 很多语言字符串使用string表示,但是Python中使用str表示字符串
help(str)
Help on class str in module builtins:
class str(object)
| str(object='') -> str
| str(bytes_or_buffer[, encoding[, errors]]) -> str
|
| Create a new string object from the given object. If encoding or
| errors is specified, then the object must expose a data buffer
| that will be decoded using the given encoding and error handler.
| Otherwise, returns the result of object.__str__() (if defined)
| or repr(object).
| encoding defaults to sys.getdefaultencoding().
| errors defaults to 'strict'.
|
| Methods defined here:
|
| __add__(self, value, /)
| Return self+value.
|
| __contains__(self, key, /)
| Return key in self.
|
| __eq__(self, value, /)
| Return self==value.
|
| __format__(self, format_spec, /)
| Return a formatted version of the string as described by format_spec.
|
| __ge__(self, value, /)
| Return self>=value.
|
| __getattribute__(self, name, /)
| Return getattr(self, name).
|
| __getitem__(self, key, /)
| Return self[key].
|
| __getnewargs__(...)
|
| __gt__(self, value, /)
| Return self>value.
|
| __hash__(self, /)
| Return hash(self).
|
| __iter__(self, /)
| Implement iter(self).
|
| __le__(self, value, /)
| Return self<=value.
|
| __len__(self, /)
| Return len(self).
|
| __lt__(self, value, /)
| Return self<value.
|
| __mod__(self, value, /)
| Return self%value.
|
| __mul__(self, value, /)
| Return self*value.
|
| __ne__(self, value, /)
| Return self!=value.
|
| __repr__(self, /)
| Return repr(self).
|
| __rmod__(self, value, /)
| Return value%self.
|
| __rmul__(self, value, /)
| Return value*self.
|
| __sizeof__(self, /)
| Return the size of the string in memory, in bytes.
|
| __str__(self, /)
| Return str(self).
|
| capitalize(self, /)
| Return a capitalized version of the string.
|
| More specifically, make the first character have upper case and the rest lower
| case.
|
| casefold(self, /)
| Return a version of the string suitable for caseless comparisons.
|
| center(self, width, fillchar=' ', /)
| Return a centered string of length width.
|
| Padding is done using the specified fill character (default is a space).
|
| count(...)
| S.count(sub[, start[, end]]) -> int
|
| Return the number of non-overlapping occurrences of substring sub in
| string S[start:end]. Optional arguments start and end are
| interpreted as in slice notation.
|
| encode(self, /, encoding='utf-8', errors='strict')
| Encode the string using the codec registered for encoding.
|
| encoding
| The encoding in which to encode the string.
| errors
| The error handling scheme to use for encoding errors.
| The default is 'strict' meaning that encoding errors raise a
| UnicodeEncodeError. Other possible values are 'ignore', 'replace' and
| 'xmlcharrefreplace' as well as any other name registered with
| codecs.register_error that can handle UnicodeEncodeErrors.
|
| endswith(...)
| S.endswith(suffix[, start[, end]]) -> bool
|
| Return True if S ends with the specified suffix, False otherwise.
| With optional start, test S beginning at that position.
| With optional end, stop comparing S at that position.
| suffix can also be a tuple of strings to try.
|
| expandtabs(self, /, tabsize=8)
| Return a copy where all tab characters are expanded using spaces.
|
| If tabsize is not given, a tab size of 8 characters is assumed.
|
| find(...)
| S.find(sub[, start[, end]]) -> int
|
| Return the lowest index in S where substring sub is found,
| such that sub is contained within S[start:end]. Optional
| arguments start and end are interpreted as in slice notation.
|
| Return -1 on failure.
|
| format(...)
| S.format(*args, **kwargs) -> str
|
| Return a formatted version of S, using substitutions from args and kwargs.
| The substitutions are identified by braces ('{' and '}').
|
| format_map(...)
| S.format_map(mapping) -> str
|
| Return a formatted version of S, using substitutions from mapping.
| The substitutions are identified by braces ('{' and '}').
|
| index(...)
| S.index(sub[, start[, end]]) -> int
|
| Return the lowest index in S where substring sub is found,
| such that sub is contained within S[start:end]. Optional
| arguments start and end are interpreted as in slice notation.
|
| Raises ValueError when the substring is not found.
|
| isalnum(self, /)
| Return True if the string is an alpha-numeric string, False otherwise.
|
| A string is alpha-numeric if all characters in the string are alpha-numeric and
| there is at least one character in the string.
|
| isalpha(self, /)
| Return True if the string is an alphabetic string, False otherwise.
|
| A string is alphabetic if all characters in the string are alphabetic and there
| is at least one character in the string.
|
| isascii(self, /)
| Return True if all characters in the string are ASCII, False otherwise.
|
| ASCII characters have code points in the range U+0000-U+007F.
| Empty string is ASCII too.
|
| isdecimal(self, /)
| Return True if the string is a decimal string, False otherwise.
|
| A string is a decimal string if all characters in the string are decimal and
| there is at least one character in the string.
|
| isdigit(self, /)
| Return True if the string is a digit string, False otherwise.
|
| A string is a digit string if all characters in the string are digits and there
| is at least one character in the string.
|
| isidentifier(self, /)
| Return True if the string is a valid Python identifier, False otherwise.
|
| Use keyword.iskeyword() to test for reserved identifiers such as "def" and
| "class".
|
| islower(self, /)
| Return True if the string is a lowercase string, False otherwise.
|
| A string is lowercase if all cased characters in the string are lowercase and
| there is at least one cased character in the string.
|
| isnumeric(self, /)
| Return True if the string is a numeric string, False otherwise.
|
| A string is numeric if all characters in the string are numeric and there is at
| least one character in the string.
|
| isprintable(self, /)
| Return True if the string is printable, False otherwise.
|
| A string is printable if all of its characters are considered printable in
| repr() or if it is empty.
|
| isspace(self, /)
| Return True if the string is a whitespace string, False otherwise.
|
| A string is whitespace if all characters in the string are whitespace and there
| is at least one character in the string.
|
| istitle(self, /)
| Return True if the string is a title-cased string, False otherwise.
|
| In a title-cased string, upper- and title-case characters may only
| follow uncased characters and lowercase characters only cased ones.
|
| isupper(self, /)
| Return True if the string is an uppercase string, False otherwise.
|
| A string is uppercase if all cased characters in the string are uppercase and
| there is at least one cased character in the string.
|
| join(self, iterable, /)
| Concatenate any number of strings.
|
| The string whose method is called is inserted in between each given string.
| The result is returned as a new string.
|
| Example: '.'.join(['ab', 'pq', 'rs']) -> 'ab.pq.rs'
|
| ljust(self, width, fillchar=' ', /)
| Return a left-justified string of length width.
|
| Padding is done using the specified fill character (default is a space).
|
| lower(self, /)
| Return a copy of the string converted to lowercase.
|
| lstrip(self, chars=None, /)
| Return a copy of the string with leading whitespace removed.
|
| If chars is given and not None, remove characters in chars instead.
|
| partition(self, sep, /)
| Partition the string into three parts using the given separator.
|
| This will search for the separator in the string. If the separator is found,
| returns a 3-tuple containing the part before the separator, the separator
| itself, and the part after it.
|
| If the separator is not found, returns a 3-tuple containing the original string
| and two empty strings.
|
| replace(self, old, new, count=-1, /)
| Return a copy with all occurrences of substring old replaced by new.
|
| count
| Maximum number of occurrences to replace.
| -1 (the default value) means replace all occurrences.
|
| If the optional argument count is given, only the first count occurrences are
| replaced.
|
| rfind(...)
| S.rfind(sub[, start[, end]]) -> int
|
| Return the highest index in S where substring sub is found,
| such that sub is contained within S[start:end]. Optional
| arguments start and end are interpreted as in slice notation.
|
| Return -1 on failure.
|
| rindex(...)
| S.rindex(sub[, start[, end]]) -> int
|
| Return the highest index in S where substring sub is found,
| such that sub is contained within S[start:end]. Optional
| arguments start and end are interpreted as in slice notation.
|
| Raises ValueError when the substring is not found.
|
| rjust(self, width, fillchar=' ', /)
| Return a right-justified string of length width.
|
| Padding is done using the specified fill character (default is a space).
|
| rpartition(self, sep, /)
| Partition the string into three parts using the given separator.
|
| This will search for the separator in the string, starting at the end. If
| the separator is found, returns a 3-tuple containing the part before the
| separator, the separator itself, and the part after it.
|
| If the separator is not found, returns a 3-tuple containing two empty strings
| and the original string.
|
| rsplit(self, /, sep=None, maxsplit=-1)
| Return a list of the words in the string, using sep as the delimiter string.
|
| sep
| The delimiter according which to split the string.
| None (the default value) means split according to any whitespace,
| and discard empty strings from the result.
| maxsplit
| Maximum number of splits to do.
| -1 (the default value) means no limit.
|
| Splits are done starting at the end of the string and working to the front.
|
| rstrip(self, chars=None, /)
| Return a copy of the string with trailing whitespace removed.
|
| If chars is given and not None, remove characters in chars instead.
|
| split(self, /, sep=None, maxsplit=-1)
| Return a list of the words in the string, using sep as the delimiter string.
|
| sep
| The delimiter according which to split the string.
| None (the default value) means split according to any whitespace,
| and discard empty strings from the result.
| maxsplit
| Maximum number of splits to do.
| -1 (the default value) means no limit.
|
| splitlines(self, /, keepends=False)
| Return a list of the lines in the string, breaking at line boundaries.
|
| Line breaks are not included in the resulting list unless keepends is given and
| true.
|
| startswith(...)
| S.startswith(prefix[, start[, end]]) -> bool
|
| Return True if S starts with the specified prefix, False otherwise.
| With optional start, test S beginning at that position.
| With optional end, stop comparing S at that position.
| prefix can also be a tuple of strings to try.
|
| strip(self, chars=None, /)
| Return a copy of the string with leading and trailing whitespace remove.
|
| If chars is given and not None, remove characters in chars instead.
|
| swapcase(self, /)
| Convert uppercase characters to lowercase and lowercase characters to uppercase.
|
| title(self, /)
| Return a version of the string where each word is titlecased.
|
| More specifically, words start with uppercased characters and all remaining
| cased characters have lower case.
|
| translate(self, table, /)
| Replace each character in the string using the given translation table.
|
| table
| Translation table, which must be a mapping of Unicode ordinals to
| Unicode ordinals, strings, or None.
|
| The table must implement lookup/indexing via __getitem__, for instance a
| dictionary or list. If this operation raises LookupError, the character is
| left untouched. Characters mapped to None are deleted.
|
| upper(self, /)
| Return a copy of the string converted to uppercase.
|
| zfill(self, width, /)
| Pad a numeric string with zeros on the left, to fill a field of the given width.
|
| The string is never truncated.
|
| ----------------------------------------------------------------------
| Static methods defined here:
|
| __new__(*args, **kwargs) from builtins.type
| Create and return a new object. See help(type) for accurate signature.
|
| maketrans(x, y=None, z=None, /)
| Return a translation table usable for str.translate().
|
| If there is only one argument, it must be a dictionary mapping Unicode
| ordinals (integers) or characters to Unicode ordinals, strings or None.
| Character keys will be then converted to ordinals.
| If there are two arguments, they must be strings of equal length, and
| in the resulting dictionary, each character in x will be mapped to the
| character at the same position in y. If there is a third argument, it
| must be a string, whose characters will be mapped to None in the result.
Python str 模块的更多相关文章
- Python标准模块--threading
1 模块简介 threading模块在Python1.5.2中首次引入,是低级thread模块的一个增强版.threading模块让线程使用起来更加容易,允许程序同一时间运行多个操作. 不过请注意,P ...
- Python标准模块--logging
1 logging模块简介 logging模块是Python内置的标准模块,主要用于输出运行日志,可以设置输出日志的等级.日志保存路径.日志文件回滚等:相比print,具备如下优点: 可以通过设置不同 ...
- python基础-模块
一.模块介绍 ...
- python logging模块可能会令人困惑的地方
python logging模块主要是python提供的通用日志系统,使用的方法其实挺简单的,这块就不多介绍.下面主要会讲到在使用python logging模块的时候,涉及到多个python文件的调 ...
- Python::OS 模块 -- 文件和目录操作
os模块的简介参看 Python::OS 模块 -- 简介 os模块的进程管理 Python::OS 模块 -- 进程管理 os模块的进程参数 Python::OS 模块 -- 进程参数 os模块中包 ...
- Day05 - Python 常用模块
1. 模块简介 模块就是一个保存了 Python 代码的文件.模块能定义函数,类和变量.模块里也能包含可执行的代码. 模块也是 Python 对象,具有随机的名字属性用来绑定或引用. 下例是个简单的模 ...
- python 各模块
01 关于本书 02 代码约定 03 关于例子 04 如何联系我们 1 核心模块 11 介绍 111 内建函数和异常 112 操作系统接口模块 113 类型支持模块 114 正则表达式 115 语言支 ...
- Python学习笔记4-如何快速的学会一个Python的模块、方法、关键字
想要快速的学会一个Python的模块和方法,两个函数必须要知道,那就是dir()和help() dir():能够快速的以集合的型式列出该模块下的所有内容(类.常量.方法)例: #--encoding: ...
- Python常用模块中常用内置函数的具体介绍
Python作为计算机语言中常用的语言,它具有十分强大的功能,但是你知道Python常用模块I的内置模块中常用内置函数都包括哪些具体的函数吗?以下的文章就是对Python常用模块I的内置模块的常用内置 ...
- Day5 模块及Python常用模块
模块概述 定义:模块,用一砣代码实现了某类功能的代码集合. 为了编写可维护的代码,我们把很多函数分组,分别放到不同的文件里,提供了代码的重用性.在Python中,一个.py文件就称之为一个模块(Mod ...
随机推荐
- Could NOT find GMP (missing: GMP_LIBRARIES)
cmake 一个开源项目的时候,一直报错.说是缺少gmp库 . 已尝试解决方法: 1.手动下载gmp库.但是官网下载的gmp库似乎没有编译成二进制文件,所以我在VS2017中写了一个简单的demo 会 ...
- PT的常用命令
归纳常用的PT命令,便于工作中应用. 1. 报时序的命令 report_timing -from A -to [get_clocks {clkA}] -delay_type min report_ ...
- 通过Rdp实现连接远程桌面
unit UnitMain; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Fo ...
- getopt函数使用说明
一.查询linux命令手册: #include<unistd.h> #include<getopt.h> /*所在头文件 */ int getopt(intargc, char ...
- windows安装WinDump
1.下载软件,放在C盘: WinDump.exehttps://www.winpcap.org/windump/install/default.htmWinPcap_4_1_3.exe(windows ...
- VS Code中使用live Server
live server可以实时查看代码更改后的变化.测试十分快速. 1.安装live server 在扩展中搜索 live server,然后点击安装.等待安装完毕进行下一步配置. 2.打开设置界面: ...
- pugixml XML格式处理库的介绍和使用(面向业务编程-格式处理)
pugixml XML格式处理库的介绍和使用(面向业务编程-格式处理) 介绍 pugixml是一个轻量级的C++ XML处理库.它的特点: 类似dom的界面,具有丰富的遍历/修改功能 非常快速的非验证 ...
- Java-10接口与抽象类
Java-10接口与抽象类 抽象方法 abstract method机制 这是一个不完整的方法,它只有一个声明,没有方法体 abstract void f(); 包含抽象方法的类被称为抽象类:如果一个 ...
- AI 脸部美容,一键让你变瘦变美变老变年轻
目录 项目效果 项目安装 安装环境 项目使用 项目效果 随着 AI 技术的发展,你不仅随时可以看到自己的老了之后的样子,还能看到自己童年的样子 随着这部分技术的开源,会有越来越多的应用,当然我觉得前景 ...
- 你的梦想家居「GitHub 热点速览」
上周推荐的 ChatGPT 版小爱,不知道有哪些小伙伴回去尝试接入了呢?本周依旧由 2 个不错的 ChatGPT 延伸项目,一个是比 DeepL.Grammarly 更懂你的划词翻译 openai-t ...