文章目录

  • 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个常用方法总结相关推荐

  1. python字符串,列表,字典的常用方法

    本篇内容 字符串的常用方法 列表的常用方法 字典的常用方法 字符串的常用方法 center 字符居中显示,指定字符串长度,填充指定的填充字符 string = "40kuai" p ...

  2. python字符串出栈方法_python字符串常用方法

    1. isalnum() :判断字符串所有的字符都是字母或者数字.返回true和false In [1]: str1='jiangwei520' In [2]: str2='jiang wei' In ...

  3. python字符串常用的方法_python字符串常用方法

    1. isalnum() :判断字符串所有的字符都是字母或者数字.返回true和false In [1]: str1='jiangwei520' In [2]: str2='jiang wei' In ...

  4. python字符串常用方法变量名命名规范

    python变量名命名规范 python变量名命名规范 1.一般以字母(大写,小写字母均可以)开头,例如:myname,Myname 2.后面可以使用下划线_或者数字 例如:My_name 3.大小写 ...

  5. 独家 | 秘籍:10个Python字符串处理技巧(附代码)

    作者:马修·梅奥 翻译:陈之炎 校对:和中华 本文约1600字,建议阅读7分钟. 本文为你介绍利用Python处理字符串的一些基本操作. 在探寻文本分析途径时却不知从何下手,该怎么办?那么可以通过这个 ...

  6. python split()方法_秘籍:10个Python字符串处理技巧(附代码)

    作者:马修·梅奥 翻译:陈之炎 校对:和中华 本文约1600字,建议阅读7分钟. 本文为你介绍利用Python处理字符串的一些基本操作. 在探寻文本分析途径时却不知从何下手,该怎么办?那么可以通过这个 ...

  7. python字符串倒数第三个_python字符串常用方法

    python 字符串常用操作方法 python 字符串操作常用操作,如字符串的替换.删除.截取.赋值.连接.比较.查找.分割等 1.去除空格 str.strip():删除字符串两边的指定字符,括号的写 ...

  8. 来不及解释!python字符串常用方法大全,先收藏再说

    目录 序言: 1.0 capitalize()函数 2.0 title()函数 3.0 swapcase()函数 4.0 lower()函数 5.0 upper()函数 6.0 casefold()函 ...

  9. 仅需10道题轻松掌握Python字符串方法 | Python技能树征题

    仅需10道题轻松掌握Python字符串方法 | Python技能树征题 0. 前言 1. 第 1 题:字符串检查 2. 第 2 题:字符串大小写转换 3. 第 3 题:字符串开头或结尾匹配 4. 第 ...

最新文章

  1. h5 和native 交互那些事儿
  2. 无法启动 MS DTC 事务管理器。LogInit 返回错误 0x2. 怎么办?
  3. MySQL双主io线程起不来_解决master and slave have equal MySQL server UUIDs导致Slave_IO_thread起不来问题...
  4. js判断字符串包含某个字符_python str 字符串的逻辑判断用法
  5. BestCoder Round #91 1001 Lotus and Characters
  6. 快速傅立叶变换_FFT
  7. is not a registered tag library. Must be one of:
  8. 微信语音怎么转发给别人_微信怎么把语音转发给别人?看看网友怎么说?原来方法这么简单...
  9. 2022年6月大学英语六级作文
  10. 详解GBase 8c数据库安全之数据透明加密
  11. Intellij IDEA 启动 Spring boot 项目在Run中启动没问题 ,Debug启动缓慢卡住 解决方式
  12. 单片机拟真电路图软件_单片机电流检测电路图大全(四款模拟电路设计原理图详解)...
  13. html5 css3 树形菜单,HTML5/CSS3卷边菜单效果
  14. 用计算机控制单片机的程序编写,单片机程序编写步骤
  15. ORB-SLAM2代码详解09: 闭环线程LoopClosing
  16. 互联网账户系统如何设计(上篇)
  17. 区块链网络端口及证书
  18. INA219 技术笔记
  19. Ganache+Truffle+MateMask实现第一个Dapp(Windows)
  20. javascript面试题(javaScript面试题)

热门文章

  1. SpringCloud工作笔记089---SpringBoot中Mybatis使用Condition_Criteria如何筛选日期类型数据
  2. memcached高速缓存学习笔记002---telnet操作memcached
  3. 图像增强(一):randaugment
  4. error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int错误的解决方法
  5. powerdesigner连接mysql,并导出其数据模型的方法
  6. Linux 重启php
  7. 设计模式之观察者模式(c++)
  8. linux 查看汉字编码方式
  9. linux下的C语言开发(信号处理)
  10. mysql操作符_MySql 中的=操作符