Python 基础知识之字符串处理
作者:ruanchao 时间:2023-04-22 05:47:55
Python字符串处理
字符串输入:
my_string = raw_input("please input a word:")
字符串判断:
(1) 判断是不是纯字母
my_string.isalpha()
字符串搜索匹配:
(1) re
re正则表达式实例一:^[\w_]*$
首先\w表示匹配包括下划线的任何单词字符,等价于'[A-Za-z0-9_]'。
然后跟了个_。
再看*号:匹配前面的子表达式零次或多次。例如,zo* 能匹配 "z" 以及 "zoo"。 * 等价于{0,}。
最后是$:表示字符串结束,后面没有其他字符了。
所以,这个表达式的意思就是把这个[\w_](任何单词字符包括下划线,然后跟个下划线)看为一个整体,出现零次或多次!
import re
my_string = raw_input("please input a word:")
if re.match('^[a-zA-Z]$', my_string):
print "it is a word"
else:
print "it is not a word"
字符串变换:
(1) 将字符串转换成全小写字母。
my_string = my_string.lower()
(2) 将多个字符串连接在一起。
my_string = my_string + "abc"
(3)截取字符串的一部分。 本示例是去掉第一个和最后一个字符,截取中间一段。
my_string = my_string[1:len(my_string)-1]
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
标签:Python,字符串,处理
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python Web框架之Django框架文件上传功能详解
2023-06-25 11:19:40
![](https://img.aspxhome.com/file/2023/5/65045_0s.jpg)
Ajax+Servlet+jsp显示搜索效果
2023-06-14 08:29:34
![](https://img.aspxhome.com/file/2023/4/65744_0s.jpg)
压缩包密码破解示例分享(类似典破解)
2022-10-17 17:03:37
python中验证码连通域分割的方法详解
2022-09-30 11:04:00
如何编写Go语言中间件的实例教程
2024-04-25 15:29:05
14个出色的jQuery导航菜单实例教程
2009-12-31 17:23:00
![](https://img.aspxhome.com/file/UploadPic/20101/11/094104n8w-71s.jpg)
对python中的pop函数和append函数详解
2021-10-09 09:11:32
![](https://img.aspxhome.com/file/2023/5/93875_0s.gif)
Python Django项目和应用的创建详解
2023-12-26 15:03:52
![](https://img.aspxhome.com/file/2023/4/127314_0s.png)
Python爬虫PyQuery库基本用法入门教程
2022-06-26 05:13:02
![](https://img.aspxhome.com/file/2023/6/110016_0s.png)
python分数表示方式和写法
2021-07-08 18:09:19
python等待10秒执行下一命令的方法
2022-02-25 00:40:03
用pywin32实现windows模拟鼠标及键盘动作
2023-07-04 21:26:30
python读取并显示图片的三种方法(opencv、matplotlib、PIL库)
2023-03-24 19:43:02
![](https://img.aspxhome.com/file/2023/9/81529_0s.jpg)
mysql 5.7.18 winx64安装配置方法图文教程
2024-01-13 16:37:30
![](https://img.aspxhome.com/file/2023/8/80128_0s.png)
关于WordPress的SEO优化相关的一些PHP页面脚本技巧
2024-06-05 09:39:16
![](https://img.aspxhome.com/file/2023/4/122534_0s.png)
SQL SERVER 触发器介绍
2024-01-14 00:27:45
Python列表常用函数使用详解
2021-08-21 01:52:05
![](https://img.aspxhome.com/file/2023/2/118342_0s.png)
python编写adb截图工具的实现源码
2021-03-24 08:50:15
![](https://img.aspxhome.com/file/2023/5/77725_0s.png)
Navicat15安装教程超详细步骤(最靠谱)
2024-01-12 22:29:11
![](https://img.aspxhome.com/file/2023/5/124495_0s.jpg)
Django路由层如何获取正确的url
2022-02-13 17:30:08