groovy 字符串截取最后一个_python 字符串 - python基础入门(12)

本文详细介绍了Python中的字符串操作,包括运算符`in`和`not in`,字符串拼接,使用`format`和`%`构造字符串,遍历字符,截取字符串,替换方法`replace()`,以及大小写转换。示例代码和输出结果展示了这些功能的实际应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

文章首发微信公众号,微信搜索:猿说python

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

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

一.字符串运算符

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

# !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))

输出结果:

hello world 字符串包含 'h'
hello world 字符串包含 'hello'
hello world 字符串不包含 'hllo'

二.字符串构造

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

# 方法一:两个字符串直接相加拼接在一起
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)

输出结果:

str3 = helloworld 
hello + world = helloworld
****************************************
str4 = 猿说python python教程 字符串 
str5 = I LOVE YOU 
****************************************
不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!

三.字符串遍历

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

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("程序结束,退出程序")

输出结果:

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开始,依次递增;

8ef2cfaae552df1771b84a05b63fccd2.png
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("程序结束,退出程序")

输出结果:

8
猿说python
p
猿说
pyt
pytho
python
猿说py
python
程序结束,退出程序

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

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

语法:

'''
字符串替换方法:替换字符串中指定的内容,并返回新的字符串
    old:字符串中需要被替换的字符或者字符串(旧字符串,原本一直就在字符串)
    new:替换之后的内容(新字符串,添加到字符串代替old的内容)
'''
 
str.replace(old, new)

代码:

str1 = "hello world"
str1 = str1.replace("hello","猿说PYTHON")
print(str1)
 
str1 = "hello world"
str1 = str1.replace("world","python 教程")
print(str1)

输出内容:

猿说PYTHON world
hello python 教程

六.字符串大小写

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

str = "www.shuopython.com"
print(str.upper()) # 把所有字符中的小写字母转换成大写字母
print(str.lower()) # 把所有字符中的大写字母转换成小写字母
print(str.capitalize()) # 把第一个字母转化为大写字母,其余小写
print(str.title()) # 把每个单词的第一个字母转化为大写,其余小写

输出结果:

WWW.SHUOPYTHON.COM
www.shuopython.com
Www.shuopython.com
Www.Shuopython.Com

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

猜你喜欢:

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

2.python变量的简单介绍

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

想了解更多python内容请直接搜索微信公众号:猿说python

Python教程 - 猿说Python​www.shuopython.com
a21c32895c8a1d2e1f1029ae9a943191.png

本人也还在学习python中,博客会持续更新ing,有兴趣的小伙伴关注走一波,推荐浏览个人博客网站:猿说python,文章采用树状分类,结构目录清晰一点,文章内容有问题的话欢迎给出建议或者直接留言.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值