python内置数据结构字符串_输入字符串hello world,用字符串对象的索引计算符及for循环

print(s[3]) #‘l’
print(s[-3]) #‘l’


![在这里插入图片描述](https://img-blog.csdnimg.cn/20210205160613933.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1N1bl9fcw==,size_16,color_FFFFFF,t_70)


### 4.切片



4.切片

“”"
切片:切出一部分的内容
s[start🔚step]
s[:end]:
s[start:]:
“”"
s = ‘hello world’
print(s[1:3]) # 从1开始到3-1结束
print(s[:3]) # 从0开始到3-1 结束
print(s[:5]) # 前五个字符
print(s[3:]) # 从第3个开始,到结束所有字符
print(s[::]) # 拷贝字符串 拿出所有字符串
print(s[::-1]) #倒序


![在这里插入图片描述](https://img-blog.csdnimg.cn/20210205162543321.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1N1bl9fcw==,size_16,color_FFFFFF,t_70)


### 5. 可迭代对象for循环访问



5. 可迭代对象for循环访问

s = ‘hello’
count = 0
for item in s:
count +=1
print(f’第{count}字符是{item}')


![在这里插入图片描述](https://img-blog.csdnimg.cn/20210205170141131.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1N1bl9fcw==,size_16,color_FFFFFF,t_70)  
 练习:  
 用户输入一个字符串,判断这个字符串是否为回文字符串。



“”"
代码需求:
用户输入一个字符串,判断这个字符串是否为回文字符串。
“”"
s = input(“输入字符串:”)
result = “回文字符串” if s == s[::-1] else “不是回文字符串”
print(s + result)


![在这里插入图片描述](https://img-blog.csdnimg.cn/20210206221605746.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1N1bl9fcw==,size_16,color_FFFFFF,t_70)


## 三、字符串的内建方法


![在这里插入图片描述](https://img-blog.csdnimg.cn/20210206222413857.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1N1bl9fcw==,size_16,color_FFFFFF,t_70)


### 1.字符串的判断和转换


下面我们演示三种 是否字母或数字? 是否数字? 是否大写字母?



s = “helloworld”
print(s.isalnum()) ##True
print(s.isdigit()) ##False
print(s.isupper()) ##False


![在这里插入图片描述](https://img-blog.csdnimg.cn/20210206222456264.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1N1bl9fcw==,size_16,color_FFFFFF,t_70)


### 2.类型转换



2.类型转换

print(‘hello’.upper()) ##HELLO
print(‘HELLO’.lower()) ## hello
print(‘HELLO world’.title()) ## Hello World
print(‘HELLO world’.capitalize()) ## Hello world
print(‘HELLO world’.swapcase()) ##hello WORLD


![在这里插入图片描述](https://img-blog.csdnimg.cn/20210206223100321.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1N1bl9fcw==,size_16,color_FFFFFF,t_70)  
 实例:



需求:用户输入Y或者y都继续执行代码

yum install httpd

choice = input (‘是否继续安装程序(y|Y):’)
if choice.lower() == ‘y’:
print(“正在安装程序…”)正在安装程序


![在这里插入图片描述](https://img-blog.csdnimg.cn/20210206223315881.png)


### 3.字符串开头和结尾的判断


![在这里插入图片描述](https://img-blog.csdnimg.cn/20210206223550308.png)



startswith

url = ‘http://www.baidu.com’
if url.startswith(‘http’):
print(f’{url}是一个正确的网址,可以爬取网站’)

endswith:

常用场景:判断文件类型

filename = ‘sun.png’
if filename.endswith(‘.png’):
print(f’{filename} 是图片文件’)
elif filename.endswith(‘.mp3’):
print(f’{filename}是音乐文件’)
else:
print(f’{filename}是未知文件’)


![在这里插入图片描述](https://img-blog.csdnimg.cn/2021020622375180.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1N1bl9fcw==,size_16,color_FFFFFF,t_70)  
 #pycharm常用的快捷键:  
 #如何查看方法的源代码和解释说明: ctrl键按住,  
 #鼠标移动到你想要查看方法的位置,点击即可进入源码及方法说明


### 4.字符串的数据清洗


![在这里插入图片描述](https://img-blog.csdnimg.cn/20210206224101378.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1N1bl9fcw==,size_16,color_FFFFFF,t_70)  
 数据清洗的思路:


* lstrip: 删除字符串左边的空格(指广义的空格: \n, \t, ’ ')
* rstrip: 删除字符串右边的空格(指广义的空格: \n, \t, ’ ')
* strip: 删除字符串左边和右边的空格(指广义的空格: \n, \t, ’ ')
* replace: 替换函数, 删除中间的空格, 将空格替换为空。replace(" ", )



s = ’ hello’
print(s)
print(s.lstrip())
s = 'hello ’
print(s.rstrip())
s = ’ hello ’
print(s.strip())
s = ‘hel lo’
print(s.replace(" “,”"))


![在这里插入图片描述](https://img-blog.csdnimg.cn/20210206225200900.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1N1bl9fcw==,size_16,color_FFFFFF,t_70)


### 5.字符串的位置调整


![在这里插入图片描述](https://img-blog.csdnimg.cn/20210206225325298.png)



print(“学生管理系统”.center(50))
print(“学生管理系统”.center(50,“*”))
print(“学生管理系统”.center(50,“-”))
print(“学生管理系统”.ljust(50,“*”))
print(“学生管理系统”.rjust(50,“*”))


效果查看:  
 ![在这里插入图片描述](https://img-blog.csdnimg.cn/20210206225837220.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1N1bl9fcw==,size_16,color_FFFFFF,t_70)


### 6.字符串的搜索和统计


![在这里插入图片描述](https://img-blog.csdnimg.cn/20210206230658390.png)



s = “hello world”
print(s.find(“llo”))
print(s.index(“llo”))
print(s.find(“xxx”))
print(s.index(“xxx”))

find如果找到子串, 则返回子串开始的索引位置。 否则返回-1

index如果找到子串,则返回子串开始的索引位置。否则报错(抛出异常).

print(s.count(“l”)) ## 判断字符出现次数


![在这里插入图片描述](https://img-blog.csdnimg.cn/20210206230717306.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1N1bl9fcw==,size_16,color_FFFFFF,t_70)


### 6.字符串的分离和拼接


![在这里插入图片描述](https://img-blog.csdnimg.cn/20210206231622709.png)



ip = ‘172.25.254.100’
print(ip.split(‘.’))
items = ip.split(‘.’)
print(‘-’.join(items))


![在这里插入图片描述](https://img-blog.csdnimg.cn/20210206231609913.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1N1bl9fcw==,size_16,color_FFFFFF,t_70)


![在这里插入图片描述](https://img-blog.csdnimg.cn/20210511152217670.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3poaWd1aWd1,size_16,color_FFFFFF,t_70)

**感谢每一个认真阅读我文章的人,看着粉丝一路的上涨和关注,礼尚往来总是要有的:**



① 2000多本Python电子书(主流和经典的书籍应该都有了)

② Python标准库资料(最全中文版)

③ 项目源码(四五十个有趣且经典的练手项目及源码)

④ Python基础入门、爬虫、web开发、大数据分析方面的视频(适合小白学习)

⑤ Python学习路线图(告别不入流的学习)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值