第七篇 python字符串(详细易懂 ^_^)

一、字符串基础

1、三引号 ,一般用于带有格式的字符串 ,一般用于文档注释或者类注释

"""
这里是文档注释
三引号声明特殊格式字符串,以及文档注释
放在最上面


"""

2、声明字符串,可以用单引号或者双引号

b = "good"
b1 = 'good'
print(b,b1)

结果
good good

3、单引号以及双引号要嵌套使用

a = "she is 'good'"
print(a)

结果
she is 'good'

4、PEP8单行字符串不要超过120字符,超过换行 用enter键或者\ 换行

s = "hello world" \
    "good"
print(s)

结果
hello worldgood

5、获取random模块的文档注释

import random
print(random.__doc__)

结果是random模块信息,内容太多,略

6、特殊字符

转义字符 \会将后续内容转义 \\第一个\是转义第二个\(或者后面的符号),告诉解释器是普通意义的字符

s7 = "网址为:198.169.10.55\\public"
print(s7)

结果
网址为:198.169.10.55\public

还有 \n  \t  \"  \'  \b

#\n
s8 = "想要换行输入\\n就行"
print(s8)
#\t 制表符
s9 = "啊啊啊啊啊\taaaaaa"
print(s9)

#\"
s10 = "打出双引号\"好好"
print(s10)

#\'
s11= '打出单引号\'1234'
print(s11)

#\b 退格键
s12 = "哈哈哈哈哈\b嘿嘿嘿嘿"
print(s12)


结果

想要换行输入\n就行
啊啊啊啊啊	aaaaaa
打出双引号"好好
打出单引号'1234
哈哈哈哈嘿嘿嘿嘿

二、字符串的遍历

1、使用+将字符串拼接

s = "hello"
s1 = "world"
print(s+s1)

结果
helloworld

2、使用*将字符串重复

s = "helloworld"
print(s*3)

结果
helloworldhelloworldhelloworld

3、索引:下标 通过索引可以找到字符串中任何字符

s0 = "hello world"
print(len(s0))#[0,len-1]  获取字符串长度
print(s0[0],s0[4])#意思是,字符串从0开始数,第0个和第四个数是几

结果
11
h o

如果查找的数不在字符串范围内,会出现下面情况

s0 = "hello world"
print(s0[0],s0[4],s0[11])



结果
 File "D:\Python2401\20.str.py", line 67
    string index out of range  :索引越界
           ^^^^^
SyntaxError: invalid syntax

string index out of range  :索引越界

4、遍历

#直接获取每个字符
input_str = input("输入字符串")
for c in input_str:
    print(c)

结果
输入字符串helloworld
h
e
l
l
o
w
o
r
l
d



# i代表每个字符的索引  s[i]代表索引i对应的字符
input_str = input("输入字符串")
for i in range(len(input_str)):
    print(i ,input_str[i])

结果
输入字符串helloworld
0 h
1 e
2 l
3 l
4 o
5 w
6 o
7 r
8 l
9 d

三、字符串中常用操作

1、format格式化字符串

a = 10
b = 20
print("a={0},b={1}".format(a,b))#格式化时用format不能用f
print(f"a={10},b={20}")

结果
a=10,b=20
a=10,b=20

2、join使用指定字符串,将可迭代(可以使用for循环遍历)内容拼接

print("+".join("hello world"))

结果
h+e+l+l+o+ +w+o+r+l+d

3、strip 剔除

print("   good    ".strip())#剔除空格
print("   good    ".lstrip())#剔除左边空格
print("   good    ".rstrip())#剔除右边空格

结果
good
good    
   good

4、count 和find


#count                          括号这一部分数字,前包含后不包含
# print("123123123123".count("123",4,9))#统计数字出现的次数  默认开始到结束位置,结束不包含
#find  找不到返回-1
# print("123123123123".find("123"))#查找123第一次出现的位置
# print("123123123123".find("123",2,9))#从2开始看123出现的的位置,结果3 从3之后开始出现123
# print("123123123123".rfind("123"))#右边第一个123的位置,从1开始数
# print("123123123123".rfind("123",2,9))#在2-9范围内,右边第一个123的位置,打印1的位置

结果
1
0
3
9
6

5、index      类似find 找不到直接报错

print("123123123".index("123"))
print("123123123".rindex("123",2,8))#包括2 不包括8

结果
0
3

6、字母格式

#首字母大写
print("hello world".capitalize())
#全部大写
print("hello world".upper())
#全部小写
print("HELLO world".lower())
#大小写切换
print("HELLo world".swapcase())
#单词首字母大写
print("hello world".title())

结果

Hello world
HELLO WORLD
hello world
hellO WORLD
Hello World

7、在指定宽度下操作,空格也可以换成字符

print("good".center(10,"*"))
#在10个字符中靠左
print("good".ljust(10,"*"))
#在10个字符中靠右
print("good".rjust(10,"*"))
#靠右用0填充
print("good".zfill(10))

结果
***good***
good******
******good
000000good

8、编码

# encode 编码
str_encode = "河南".encode(encoding="GB2312")
print(str_encode)
str_encode = "中国".encode(encoding="GB2312")
print(str_encode)
#decode 解码
print(str_encode.decode(encoding="GB2312"))

结果
b'\xba\xd3\xc4\xcf'
b'\xd6\xd0\xb9\xfa'
中国

9、startswith 开始     endswith  结束

#startswith 开始  看startswith()里的字符是不是开头
print("中华人民共和国".startswith("中"))
#endswith 结束  看endswith()里的字符是不是结束
print("中华人民共和国".endswith("国"))
#替换
print("中华人民共和国".replace("中华","中国"))


结果
True
True
中国人民共和国

10、判断字母

# isalpha是否全是字母
print("abc1".isalpha())#false
print("abc中".isalpha())#True
#全数字
print("1a".isalnum())#True
#字母或数字
print("abc123".isalnum())#True


你可能感兴趣的:(python)