运行环境
python:3.8.3
jupyter-notebook : 6.4.0
注意:本文案例可以直接在 jupyter-notebook
上运行,但在 PyCharm
上的话需要代码的最后一句加上 print
哦!
输入输出函数
print()
print()
无疑是我们使用最多的函数,他可以直接输出、指定间隔/结尾字符、将输出内容保存到指定文件(应用:记录自动化脚本异常信息)等。下面列举它的常见用法。
1️⃣ 直接输出
print('hello world') 复制代码
output:hello world |
2️⃣ 指定间隔字符sep
print('A', 'B', 'C', sep=' Python ') 复制代码
output:A Python B Python C |
3️⃣ 指定结尾字符
print('hello', 'world', end='Python') 复制代码
output:hello worldPython |
4️⃣ 将输出内容保存到outfile.txt文件中
print('hello', 'world', sep=' ', file=open('outfile.txt', 'w', encoding='utf-8')) 复制代码
input()
input()
可以接收用户输入的内容,并以字符串的形式保存。
name = input('name:') 复制代码
在 jupyter notebook
上执行的效果可能和别的编辑器不同,但操作都是输入完后,按 “回车” 即可。
获取数据类型
type()
type()
返回指定值的数据类型。
type([1, 2]) 复制代码
output:list |
isintance()
isintance()
判断传入的值是否为指定类型,返回 True/False
。
isinstance('Python新视野', str) 复制代码
output:True |
字符串操作
str()
str()
将指定值转为字符串类型。
str(1.23) 复制代码
output:'1.23' |
eval()
eval()
将字符串转成有效的表达式来求值或者计算结果。可以将字符串转化成列表(list
),元组(tuple
),字典(dict
),集合(set
)等。
res = eval("{'name': 'Python'}") type(res) 复制代码
output:dict |
str.capitalize()
capitalize()
返回字符串中的首字母大写,其余小写的字符串
cap_str = 'python新视野'.capitalize() cap_str 复制代码
output:'Python新视野' |
str.center()
center()
返回一个指定宽度的居中字符串,左右部分空余部分用指定字符填充。
width
:长度fillchar
:空余部分填充的字符,默认使用空格
center_str = 'Python新视野'.center(15, "!") center_str 复制代码
output:'!!!Python新视野!!!' |
str.count()
str.count(sub, start, end)
返回 sub
在 str
中出现的次数,可以通过 [start, end]
指定范围,若不指定,则默认查找整个字符串。
- sub: 子串
- start: 开始的索引,默认为 0
- end: 结束的索引,默认为字符串的长度
name = 'python python' # 第一次按默认范围统计'p'出现的次数, # 第二次指定start=1,即从第二个字符开始统计。 name.count('p'), name.count('p', 1) 复制代码
output:(2, 1) |
str.find() & str.rfind()
1️⃣find()
从左往右扫描字符串,返回 sub
第一次出现的下标。可以通过 [start, end]
指定范围,若不指定,则默认查找整个字符串。如最后未找到字符串则返回 -1。
- sub: 子串
- start: 开始检索的位置,默认为 0
- end: 结束检索的位置,默认为字符串的长度
name = 'Python' # 第一次按默认范围查找'Py'第一次出现的下标 # 第二次指定start=1,即从第二个字符开始查找。 name.find('Py'), name.find('Py', 1) 复制代码
output:(0, -1) |
2️⃣rfind
与 find()
的用法相似,只是从右往左开始扫描,即从字符串末尾向字符串首部扫描。
name = 'Python' name.rfind('Py'), name.rfind('Py', 1) 复制代码
output:(0, -1) |
str.index() & str.rindex()
1️⃣index()
和 find()
用法相同,唯一的不同是如果找不到 sub
会报错。
示例🅰️
name = 'Python' name.index('Py', 0) 复制代码
output:0 |
示例 :b:
name = 'Python' name.index('Py', 1) 复制代码
output:ValueError: substring not found |
2️⃣rindex()
和 index()
用法相同,不过是从右边开始查,它的查询与 index()
相同。
name = 'Python' name.rindex('Py', 0) 复制代码
output:0 |
str.isalnum()
isalnum()
判断字符串中是否所有字符都是字母(可以为汉字)或数字,是 True
,否 False
,空字符串返回 False
。
示例🅰️
'Python新视野'.isalnum() 复制代码
output:True |
示例 :b:
'Python-sun'.isalnum() 复制代码
output:False |
'-' 是符号,所以返回 False 。 |
str.isalpha()
isalpha()
判断字符串中是否所有字符都是字母(可以为汉字),是 True
,否 False
,空字符串返回 False
。
示例🅰️
'Python新视野'.isalpha() 复制代码
output:True |
示例🅱️
'123Python'.isalpha() 复制代码
output:False |
其中包含了数字,返回 False |
str.isdigit()
isdigit()
判断字符串中是否所有字符都是数字(Unicode数字,byte数字(单字节),全角数字(双字节),罗马数字),是 True
,否 False
,空字符串返回 False
。
示例🅰️
'四123'.isdigit() 复制代码
output:False |
其中包含了汉字数字,返回 False
示例🅱️
b'123'.isdigit() 复制代码
output:True |
byte
数字返回 True
。
str.isspace()
字符串中只包含空格(\n
、\r
、\f
、\t
、\v
),是 True
,否 False
,空字符串返回 False
。
符号 | 含义 |
\n | 换行 |
\r | 回车 |
\f | 换页 |
\t | 横向制表符 |
\v | 纵向制表符 |
' \n\r\f\t\v'.isspace() 复制代码
output:True |
str.join()
join(iterable)
以指定字符串作为分隔符,将 iterable
中所有的元素(必须是字符串)合并为一个新的字符串。
','.join(['Python', 'Java', 'C']) 复制代码
output:'Python,Java,C' |
str.ljust() & str.rjust()
1️⃣ljust()
返回一个指定宽度左对齐的字符串
width
:长度fillchar
:右部空余部分填充的字符,默认使用空格
ljust_str = 'Python新视野'.ljust(15, "!") ljust_str 复制代码
output:'Python新视野!!!!!!' |
2️⃣rjust()
返回一个指定宽度右对齐的字符串,与 ljust
操作正好相反。
width
:长度fillchar
:左部空余部分填充的字符,默认使用空格
rjust_str = 'Python新视野'.rjust(15, "!") rjust_str 复制代码
output:'!!!!!!Python新视野' |
str.lower() & str.islower()
1️⃣lower()
将指定字符串转换为小写。
lower_str = 'Python新视野'.lower() lower_str 复制代码
output:'python新视野' |
2️⃣islower()
判断字符串所有区分大小写的字符是否都是小写形式,是 True
,否 False
,空字符串或字符串中没有区分大小写的字符返回 False
。
'python-sun'.islower() 复制代码
output:True |
'python-sun'
区分大小写的字符有 'pythonsun'
,并且都是小写,所以返回 True
。
str.lstrip() & str.rstrip() & str.strip()
1️⃣lstrip()
会在字符串左侧根据指定的字符进行截取,若未指定默认截取左侧空余(空格,\r,\n,\t等)部分。
name = '+++Python新视野+++' name.lstrip('+') 复制代码
output:'Python新视野+++' |
2️⃣rstrip()
与 lstrip()
用法相似,只是截取右侧的内容。
name = '+++Python新视野+++' name.rstrip('+') 复制代码
output:'+++Python新视野' |
3️⃣strip()
实际是 lstrip()
与 rstrip()
的结合,它会截取字符串两边指定的字符。
name = '+++Python新视野+++' name.strip('+') 复制代码
output:'Python新视野' |
str.split() & str.splitlines()
1️⃣str.split(sep=None, maxsplit=-1)
使用 sep
作为分隔符将字符串进行分割,返回字符串中的单词列表。
- seq: 用来分割字符串的分隔符。
None
(默认值)表示根据任何空格进行分割,返回结果中不包含空格。 - maxsplit: 指定最大分割次数。-1(默认值)表示不限制。
split_str = 'P y t h o n 新 视 野' split_str.split(maxsplit=2) 复制代码
output:['P', 'y', 't h o n 新 视 野'] |
使用默认的空格进行分割,设置最大的分割次数为2
2️⃣str.splitlines
返回字符串中的行列表,它按照行 ('\r',\n','\r\n')
分隔,返回分隔后的列表。它只有一个参数 keepends
表示是否在结果中保留换行符,False
(默认)不保留,True
保留。
示例🅰️
split_str = 'P\ny\r t h o n 新 视 野' split_str.splitlines() 复制代码
output:['P', 'y', ' t h o n 新 视 野'] |
示例🅱️
split_str = 'P\ny\r t h o n 新 视 野' split_str.splitlines(keepends=True) 复制代码
output:['P\n', 'y\r', ' t h o n 新 视 野'] |
str.startswith() & str.endswith
1️⃣startswith(prefix[, start[, end]])
检查字符串是否是以指定子字符串 substr
开头,是 True
,否 False
,空字符串会报错。如果指定 start
和 end
,则在指定范围内检查。
startswith_str = 'Python新视野' startswith_str.startswith('thon', 2) 复制代码
output:True |
从第 3 个字符开始检测 |
2️⃣str.endswith(suffix[, start[, end]])
与 startswith
用法相同,不同之处是检查字符串是否以指定子字符串结尾,是 True
,否 False
,空字符串会报错。
endswith_str = 'Python新视野' endswith_str.endswith('thon', 0, 6) 复制代码
output:True |
从第 1 个字符开始检测,到第 7 个字符结束(不包含第 7 个),注意这里的范围和字符串切片其实是一样的道理,都是前闭后开。 |
str.title() & str.istitle()
1️⃣title()
返回字符串中每一个单词首字母大写。
title_str = 'python新视野 python新视野'.title() title_str 复制代码
output:'Python新视野 Python新视野' |
2️⃣istitle()
判断字符串是否满足每一个单词首字母大写,是 True
,否 False
,空字符串返回 False
。
'Abc Def '.istitle() 复制代码
output:True |
str.upper() & str.isupper()
1️⃣upper()
将指定字符串中字母转换为大写。
upper_str = 'Python新视野'.upper() upper_str 复制代码
output:'PYTHON新视野' |
2️⃣isupper()
判断字符串所有区分大小写的字符是否都是大写形式,是 True
,否 False
,空字符串或字符串中没有区分大小写的字符返回 False
。
'PYTHON-SUN'.isupper() 复制代码
output:True |