首页 > 编程语言 > 详细

Python_base_字符串

时间:2018-12-13 23:09:49      阅读:196      评论:0      收藏:0      [点我收藏+]
一、字符串查找和替换

hello_str="hello word"


#判断是否以指定字符串开始
print(hello_str.startswith("hello")) True

#判断是否以指定字符串结束
print(hello_str.endswith("word")) True

#查找指定字符串
print(hello_str.find("llo")) 2
print(hello_str.find("lloc")) -1 #index指定的字符串会报错,find指定的字符串不存在会返回-1

#替换字符串
#replace方法执行完成后,会返回一个新的字符串,不会修改原有字符串的内容
print(hello_str.replace("hello","python")) python word

print(hello_str) hello word

二、去除空白字符

pome=["\t\n慈母手中线",
"游子身上衣",
"临行密密缝",
"意恐迟迟归"]

for pome_str in pome:

print("|%s|" %pome_str.strip().center(10))
#先使用strip方法去除字符串中的空白字符
#再用center方法居中显示文本


输出结果:

| 慈母手中线 |
| 游子身上衣 |
| 临行密密缝 |
| 意恐迟迟归 |

 



 

Python_base_字符串

原文:https://www.cnblogs.com/tianpin/p/10116923.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!