Python基础(02)——字符串
Python基础(02)——字符串
1. 字符串的定义
文本类型也就是所说的字符串,给变量赋值时使用单引号或者双引号进行赋值,如:x = "hello World",如果文本内容过多,还可以是用三对单引号或者三对双引号进行赋值。切记:python中的字符串是不可变的!!!
x = """"
text,text
"""
2. 字符串数组
1.字符串数组: ,可以用序号表示字符位置,相当于一个数组。可以用方括号访问字符串的元素。如a[1]
2.裁切:
格式:字符串[开始索引:结束索引:步长]
从左到右截取其中一段字符。如a[1:5],获取位置是从左到右1-4(不包含5)。
从右到左截取其中一段字符。如a[-5:-2],获取位置从右到左2-4(不包含5)
切记:字符串的第一个字符下标为0。
截取时步长默认为1。截取时如果不写开始索引,则默认从头下标0开始。如果不写结束索引,则默认到末尾结束。
a = 'Hello world!'
print(a[1]) #输出e
print(a[1:5]) #输出ello 左闭右开
print(a[-5:-2]) #输出orl 左开右闭
print(a[:2]) #输出He
print(a[6:]) #输出world!
print(a[1:8:2]) #输出el,o
3. 字符串常用方法
字符串操作方法
以下方法都是基于a = "Hello,World!"的执行的
len():len(a) 返回字符串长度
strip():a.strip() 删除开头和结尾的空白字符。
lstrip():a.lstrip() 删除开头的空白字符。
rstrip():a.rstrip() 删除结尾的空白字符。
lower():a.lower() 返回小写的字符串。
upper(): a.upper() 返回大写的字符串。
replace(): a.replace("旧字符串","新字符串") 用另一段字符串来替换字符串。
split():a.aplit(",") 找到分隔符的实例时将字符串分割成两个子字符串。返回:['Hello', 'world!']
titile(): a.titile() 将字符串中每个单词的首字母改成大写
capitalize(): a.capitalize() 将字符串的首字母改正大写
find(): a.find("o") 查找字母“o"在字符串中首次出现的位置下标,若未出现过则返回-1
index(): a.index("llo") 查找指定子串在字符串中出现的位置,返回首个字符下标。如果没有则引发 ValueError错误
translate(): 使用制定指定的翻译表对字符串进行相对应的替换,返回替换后的字符串
a = 'Hello,world!'
print(len(a)) #12
print(a.strip()) #Hello,world!
print(a.lstrip()) #Hello,world!
print(a.rstrip()) #Hello,world!
print(a.lower()) #hello,world!
print(a.upper()) #HELLO,WORLD!
print(a.replace("world","Human")) #Hello,Human!
print(a.split(",")) #['Hello', 'world!']
print(a.title()) #Hello,World!
print(a.capitalize()) #Hello,world!
print(a.find("o")) #4
print(a.index("llo")) #2
tranTable=a.maketrans("Hello,World!","HELLO,WORLD!") #制作翻译表
print(a.translate(tranTable)) #HELLO,WORLD!
字符串判断方法
isalnum() | 如果字符串中的所有字符都是字母数字,则返回 True。 |
---|
isalnum() | 如果字符串中的所有字符都是字母数字,则返回 True。 |
---|
isalnum() | 如果字符串中的所有字符都是字母数字,则返回 True。 |
---|
isalnum() | 如果字符串中的所有字符都是字母数字,则返回 True。 |
---|
isalnum() | 如果字符串中的所有字符都是字母数字,则返回 True。 |
---|
isalnum() | 如果字符串中的所有字符都是字母数字,则返回 True。 |
---|
isalnum() | 如果字符串中的所有字符都是字母数字,则返回 True。 |
---|
isalnum() | 如果字符串中的所有字符都是字母数字,则返回 True。 |
---|
isalnum() | 如果字符串中的所有字符都是字母数字,则返回 True。 |
---|
isalnum() | 如果字符串中的所有字符都是字母数字,则返回 True。 |
---|
isalnum() | 如果字符串中的所有字符都是字母数字,则返回 True。 |
---|
4. 字符串格式化
python对字符串格式化有两种方式;1.通过%来进行格式化。2.通过format方法进行格式化
4.1 %方式格式化
常用转换说明符:
d:转换为带符号的十进制形式的整数
f:转换为十进制形式的浮点数
s:使用str()将变量或表达式转换为字符串
可以通过%.数字来指定小鼠先之后的数字位数,如果时字符串,则表示字符串可以存放大最大字符数。这个表示称之为精度值,
num = -10
string = "Hello"
print("The number is %d." %num)
print("The floatNumber is %.3f" %num)
print("The text is %s" %string)
print("The text is %.3s" %string)
#output
# The number is -10.
# The floatNumber is -10.000
# The text is Hello
# The text is Hel
还可以通过字典对字符串进行格式化
dic = {'name':'Jing',
'age':18}
print("My name is %(name)s and %(age)d years old" %(dic))
#output
#My name is Jing and 18 years old
4.2 format方式格式化
str.format() format格式化有三种方式:
1.{}占位符
2.{0},{1}带有编号的占位符
3.{关键字}带关键字的占位符
name = 'Jing'
age = 20
university = "DMU"
print("My name is {} and {} years old,graduate from {}".format(name,age,university))
print("My name is {0} and {1} years old,graduate from {2}".format(name,age,university))
print("My name is {Name} and {Age} years old,graduate from {University}"
.format(Name=name,Age=age,University=university))