温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

python 字符串(12)

发布时间:2020-06-15 00:54:32 来源:网络 阅读:238 作者:qq5d6f345f0205e 栏目:编程语言

在 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字符串


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI