python 字符串(12)
admin
2023-06-28 08:23:25
0次

在 python变量 文章中我们对python变量做了一个简单的了解,整数/浮点数/bool值相对来讲都比较简单,今天详细在讲解一下关于字符串的内容,字符串俗称:str。

在本文会大量的使用print 和format 函数,如果还有不太熟悉使用的盆友,请先预习:关于python开发中print 函数和format 函数详细解释

 

一.字符串运算符

介绍两个关于python字符串的运算符,”in” 和 “not in”,主要用于检测字符串中是否存在某个字符或者字符串,如果存在返回True,不存在返回False,直接上代码演示:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

# !usr/bin/env python

# -*- coding:utf-8 _*-

"""

@Author:何以解忧

@Blog(个人博客地址): shuopython.com

@WeChat Official Account(微信公众号):猿说python

@Github:www.github.com

@File:string123.py

@Time:2019/9/23 20:45

 

@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!

"""

 

# 检测单个字符

str1 = "hello world"

if "h" in str1:

    print("{} 字符串包含 'h'".format(str1))  # 注意单引号和双引号的配合使用

else:

    print("{} 字符串不包含 'h'".format(str1))

 

# 检测字符串

if "hello" in str1:

    print("{} 字符串包含 'hello'".format(str1))  # 注意单引号和双引号的配合使用

else:

    print("{} 字符串不包含 'hello'".format(str1))

 

# 使用 not in

if "hllo" not in str1:

    print("{} 字符串不包含 'hllo'".format(str1))  # 注意单引号和双引号的配合使用

else:

    print("{} 字符串包含 'hllo'".format(str1))

输出结果:

1

2

3

hello world 字符串包含 'h'

hello world 字符串包含 'hello'

hello world 字符串不包含 'hllo'

 

二.字符串构造

字符串可以直接拼接,同样也可以使用format 函数或者 % 符号构造,代码如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

# 方法一:两个字符串直接相加拼接在一起

str1 = "hello"

str2 = "world"

str3 = str1 + str2

print("str3 = %s " % str3)

print("{} + {} = {}".format(str1,str2,str3))

 

print("**"*20)

# 方法二:使用format

str4 = "{} {} {}".format("猿说python","python教程","字符串")

print("str4 = %s " % str4)

str5 = "{2} {1} {0}".format("YOU","LOVE","I")  # 注意使用下标索引值,默认重0开始

print("str5 = %s " % str5)

 

print("**"*20)

# 方法三:使用 % 符号 ,% 使用方法与print类似

str6 = "%s%s%s" % ("不积跬步无以至千里",",不积小流无以成江海",

                   ",程序人生的精彩需要坚持不懈地积累!")

print(str6)

输出结果:

1

2

3

4

5

6

7

str3 = helloworld

hello + world = helloworld

****************************************

str4 = 猿说python python教程 字符串

str5 = I LOVE YOU

****************************************

不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!

 

三.字符串遍历

可以通过for循环或者while循环遍历字符串中的每一个字符,在下面代码中有一个内置函数len()函数,用于获取字符串长度,代码如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

str1 = "hello world"

print("%s 字符串总长度:%d" % (str1,len(str1))) # len()获取字符串长度

 

#方法一:

for i in str1:

    print(i,end="-")  # print 函数默认换行,强制将换行符改为 '-',可以改为任意字符

    

print("\n") # "\n" 表示换行

print("*"*20)

 

#方法二:

for i in range(0,len(str1)):

    print(str1[i],end=' ') # 每个字符以空格隔开

 

print("\n") # "\n" 表示换行

print("*"*20)

 

#方法三:

a = 0

while a < len(str1):

    print("str[%d] = %s " % (a,str1[a]))

    a += 1

print("程序结束,退出程序")

输出结果:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

hello world 字符串总长度:11

h-e-l-l-o- -w-o-r-l-d-

 

********************

h e l l o   w o r l d

 

********************

str[0] = h

str[1] = e

str[2] = l

str[3] = l

str[4] = o

str[5] =  

str[6] = w

str[7] = o

str[8] = r

str[9] = l

str[10] = d

程序结束,退出程序

 

四.字符串截取

字符串中的每一个字符都有一个默认的索引值,从左到右默认重0开始,依次递增;从右往左默认重-1开始,依次递增;

python 字符串(12)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

str1 = "猿说python"

print(len(str1))            # 内置函数 len() 获取字符串长度

print(str1)                 # 打印字符串

print(str1[2])              # 获取字符串中的第二个字符

print(str1[0:2])            # 截取字符串索引值为0~1的字符,不包括索引值为2的字符

print(str1[2:5])            # 截取字符串索引值为2~4的字符,不包括索引值为5的字符

print(str1[2:-1])           # 截取字符串重索引值为2开始直到字符串结尾的前一个,-1的索引值表示最后一个

print(str1[2:len(str1)])    # 截取字符串索引值2~8,最后一个字符的索引值为7,所以刚刚好能截取到字符串末尾

 

# 截取在列表中索引值为0-4的数据,冒号前面不设置参数,默认重0开始,注意截取并不包括4

print(str1[:4])

# 截取在列表中索引值为2-末尾的数据,冒号后面不设置参数,默认截取到最后一位数据,注意截取包括最后一位            

print(str1[2:])            

 

print("程序结束,退出程序")

输出结果:

1

2

3

4

5

6

7

8

9

10

8

猿说python

p

猿说

pyt

pytho

python

猿说py

python

程序结束,退出程序

注意:在上面 print(str1[2:-1]) 改行代码中,-1 表示最后一位字符串索引,但是截取的范围并不包括字符串的最后一位。

 

五.字符串替换 – replace()方法

语法:


1

2

3

4

5

6

7

'''

字符串替换方法:替换字符串中指定的内容,并返回新的字符串

    old:字符串中需要被替换的字符或者字符串(旧字符串,原本一直就在字符串)

    new:替换之后的内容(新字符串,添加到字符串代替old的内容)

'''

 

str.replace(old, new)


代码:


1

2

3

4

5

6

7

str1 = "hello world"

str1 = str1.replace("hello","猿说PYTHON")

print(str1)

 

str1 = "hello world"

str1 = str1.replace("world","python 教程")

print(str1)

输出内容:

1

2

猿说PYTHON world

hello python 教程

 

六.字符串大小写

对字符串进行大小写转换处理

1

2

3

4

5

str = "www.shuopython.com"

print(str.upper())          # 把所有字符中的小写字母转换成大写字母

print(str.lower())          # 把所有字符中的大写字母转换成小写字母

print(str.capitalize())     # 把第一个字母转化为大写字母,其余小写

print(str.title())          # 把每个单词的第一个字母转化为大写,其余小写

输出结果:

1

2

3

4

WWW.SHUOPYTHON.COM

www.shuopython.com

Www.shuopython.com

Www.Shuopython.Com

关于字符串的函数还有很多,由于篇幅有限,后面的文章我们继续讲解更多关于python字符串相关函数。

 

猜你喜欢:

1.python print 和format详细使用教程

2.python变量的简单介绍

 

转载请注明:猿说Python » python字符串


相关内容

热门资讯

空调连续工作多久需要停机休息 空调连续工作的时间取决于多个因素,包括空调的型号、功率、使用环境和维护情况等。一般来说,空调可以连续...
空调哪个符号是除湿的 空调除湿功能通常用一个水滴形状的符号表示。不同品牌和型号的空调遥控器可能会有所差异,但一般来说,除湿...
格力空调多长时间加一次氟利昂 1、一般情况下,空调对氟利昂的使用量是非常小的,所以在空调使用了四年左右的时候再考虑加也没问题。2、...
空调用多长时间需要加氟一次 一般情况下是五年。当出现以下这几种情况时,空调才需要加氟。1、空调的室内机和室外机各有两个接口,当接...
格调生活·C标思享荟:卡萨帝在... 9月12日的郑州龙湖金融岛,夜风轻拂,灯光璀璨。“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑...
品牌价值破千亿、高端份额持续领... 9月12日,“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑州站”在郑州龙湖金融岛举办。2026...
空调主板坏了修800还是换新的 空调主板坏了是修还是换新,需要考虑多个因素,以下是一些建议:1. 维修成本:如果维修费用较低,比如 ...
空调开多久需要休息多长时间 问题:空调开多久需要休息多长时间回答:1、在使用空调的过程中,一般空调开7~8个小时左右需要休息2~...
中央空调多长时间清洗一次 1、一般空调开机前清洗一次即可,空调开机中间时段再清洗一次,空调关机时清洗一次,这样比较合理。2、空...
中央空调氟利昂多久加一次 看空调制热和制冷程度,很多人的观点认为氟利昂必须一年放一次,其实这个观点是错误的,理解上面有点偏差,...