python字符串的10个常用方法总结
文章目录
- 1 字符串用 + 和 * 连接
- 2 len(string)——计算字符串的长度
- 3 string[left,right]——字符串的分片与索引
- 4 string.find(sub_string)——查找子字符串出现的位置
- 5 string.replace(string_a,string_b)——替换部分字符串
- 6 str(int_a)——强制类型转换
- 7 '{} and {}'.format(string_a,string_b)——字符串格式化符
- 8 strings.split()——分割字符串中的单词
- 9 strings.count(string)——统计字符string出现的次数
- 10 strings.strip(string.punctuation).lower()——忽略字符,转换小写
1 字符串用 + 和 * 连接
用+
连接
what_he_does = ' plays '
his_instrument = 'guitar'
his_name = 'Robert Johnson'
artist_intro = his_name + what_he_does + his_instrument
print(artist_intro)# echo
Robert Johnson plays guitar
用*
连接
word = 'love'
words = word*3
print(words)# echo
lovelovelove
2 len(string)——计算字符串的长度
word = 'love'
words = word*3
print(len(words))# echo
12
3 string[left,right]——字符串的分片与索引
字符串有点像一个元组,所以可以用string[index]
的方式进行索引
print(name[5:]) # 右侧不写,代表到最后
'me is Mike'
print(name[:5]) # 注意是左闭右开,所以第5个是取不到的
'My na'
从左到右,是和数组一样,从0开始的,而从右往左是从-1开始,具体索引方式如下
4 string.find(sub_string)——查找子字符串出现的位置
sub_num = '123'
phone_num = '138-1034-5123'
print(phone_num.find(sub_num))# echo
10 # 返回第一次出现123这个字符串的位置,10是1的位置
5 string.replace(string_a,string_b)——替换部分字符串
phone_num = '138-1034-5123'
phone_num_new = phone_num.replace(phone_num[9:13],'*'*4)
print(phone_num_new)# echo
138-1034-****
6 str(int_a)——强制类型转换
sub_num = '123'
phone_num = '138-1034-5123'
# 用type()函数确定变量类型
print(phone_num.find(sub_num),type(phone_num.find(sub_num)))
print(str(phone_num.find(sub_num)),type(str(phone_num.find(sub_num))))# echo
10 <class 'int'> # find()返回的是int类型
10 <class 'str'> # str()强制转换成string字符串类型
7 ‘{} and {}’.format(string_a,string_b)——字符串格式化符
a = 'I'
b = 'you'
print('{} love {} forever.'.format(a,b))# echo
I love you forever.
8 strings.split()——分割字符串中的单词
lyric = 'The night begin to shine, the night begin to shine'
words = lyric.split()
print(words)# echo
['The', 'night', 'begin', 'to', 'shine,', 'the', 'night', 'begin', 'to', 'shine'] # 注意:'shine,'后面跟了一个逗号!
默认split()
是根据空格划分的,可以使用split('\n')
根据换行来分割
fn.append(line.split('\n')[0]) # [0]表示分割后list的第一个元素
9 strings.count(string)——统计字符string出现的次数
lyric = 'The night begin to shine, the night begin to shine'
words = lyric.split()
word = 'night'
print(words.count(word))# echo
2 # 'night'出现了两次
10 strings.strip(string.punctuation).lower()——忽略字符,转换小写
string.punctuation
包含以下的特殊字符:
!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
使用strip(ch)
可以忽略ch
这些字符
string.lower()
可以将字符串全部转换成小写。
import string
lyric = 'The night begin to shine, the night begin to shine'
# 首先把字符串按照空格分开,切割成包含一个个string的list
# 接着使用列表解析式,对每个元素进行1)忽略特殊字符2)转换成小写单词
words = [word.strip(string.punctuation).lower() for word in lyric.split()]
print(words)# echo
['the', 'night', 'begin', 'to', 'shine', 'the', 'night', 'begin', 'to', 'shine']
python字符串的10个常用方法总结相关推荐
- python字符串,列表,字典的常用方法
本篇内容 字符串的常用方法 列表的常用方法 字典的常用方法 字符串的常用方法 center 字符居中显示,指定字符串长度,填充指定的填充字符 string = "40kuai" p ...
- python字符串出栈方法_python字符串常用方法
1. isalnum() :判断字符串所有的字符都是字母或者数字.返回true和false In [1]: str1='jiangwei520' In [2]: str2='jiang wei' In ...
- python字符串常用的方法_python字符串常用方法
1. isalnum() :判断字符串所有的字符都是字母或者数字.返回true和false In [1]: str1='jiangwei520' In [2]: str2='jiang wei' In ...
- python字符串常用方法变量名命名规范
python变量名命名规范 python变量名命名规范 1.一般以字母(大写,小写字母均可以)开头,例如:myname,Myname 2.后面可以使用下划线_或者数字 例如:My_name 3.大小写 ...
- 独家 | 秘籍:10个Python字符串处理技巧(附代码)
作者:马修·梅奥 翻译:陈之炎 校对:和中华 本文约1600字,建议阅读7分钟. 本文为你介绍利用Python处理字符串的一些基本操作. 在探寻文本分析途径时却不知从何下手,该怎么办?那么可以通过这个 ...
- python split()方法_秘籍:10个Python字符串处理技巧(附代码)
作者:马修·梅奥 翻译:陈之炎 校对:和中华 本文约1600字,建议阅读7分钟. 本文为你介绍利用Python处理字符串的一些基本操作. 在探寻文本分析途径时却不知从何下手,该怎么办?那么可以通过这个 ...
- python字符串倒数第三个_python字符串常用方法
python 字符串常用操作方法 python 字符串操作常用操作,如字符串的替换.删除.截取.赋值.连接.比较.查找.分割等 1.去除空格 str.strip():删除字符串两边的指定字符,括号的写 ...
- 来不及解释!python字符串常用方法大全,先收藏再说
目录 序言: 1.0 capitalize()函数 2.0 title()函数 3.0 swapcase()函数 4.0 lower()函数 5.0 upper()函数 6.0 casefold()函 ...
- 仅需10道题轻松掌握Python字符串方法 | Python技能树征题
仅需10道题轻松掌握Python字符串方法 | Python技能树征题 0. 前言 1. 第 1 题:字符串检查 2. 第 2 题:字符串大小写转换 3. 第 3 题:字符串开头或结尾匹配 4. 第 ...
最新文章
- h5 和native 交互那些事儿
- 无法启动 MS DTC 事务管理器。LogInit 返回错误 0x2. 怎么办?
- MySQL双主io线程起不来_解决master and slave have equal MySQL server UUIDs导致Slave_IO_thread起不来问题...
- js判断字符串包含某个字符_python str 字符串的逻辑判断用法
- BestCoder Round #91 1001 Lotus and Characters
- 快速傅立叶变换_FFT
- is not a registered tag library. Must be one of:
- 微信语音怎么转发给别人_微信怎么把语音转发给别人?看看网友怎么说?原来方法这么简单...
- 2022年6月大学英语六级作文
- 详解GBase 8c数据库安全之数据透明加密
- Intellij IDEA 启动 Spring boot 项目在Run中启动没问题 ,Debug启动缓慢卡住 解决方式
- 单片机拟真电路图软件_单片机电流检测电路图大全(四款模拟电路设计原理图详解)...
- html5 css3 树形菜单,HTML5/CSS3卷边菜单效果
- 用计算机控制单片机的程序编写,单片机程序编写步骤
- ORB-SLAM2代码详解09: 闭环线程LoopClosing
- 互联网账户系统如何设计(上篇)
- 区块链网络端口及证书
- INA219 技术笔记
- Ganache+Truffle+MateMask实现第一个Dapp(Windows)
- javascript面试题(javaScript面试题)
热门文章
- SpringCloud工作笔记089---SpringBoot中Mybatis使用Condition_Criteria如何筛选日期类型数据
- memcached高速缓存学习笔记002---telnet操作memcached
- 图像增强(一):randaugment
- error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int错误的解决方法
- powerdesigner连接mysql,并导出其数据模型的方法
- Linux 重启php
- 设计模式之观察者模式(c++)
- linux 查看汉字编码方式
- linux下的C语言开发(信号处理)
- mysql操作符_MySql 中的=操作符