Python入门教程第一节:如何进行字符串基本操作?
- 内容介绍
- 文章标签
- 相关推荐
本文共计1819个文字,预计阅读时间需要8分钟。
Python基础学习之字符串操作+字符串是编程中最常见的类型,因此掌握好字符串的常用操作方法是非常必要的!
1.字符串的切片+[]+字符串的切片是最常见的字符串操作,必须熟练掌握;
Python基础学习之字符串操作
字符串是编程中最常遇到的类型,所以掌握好字符串的常用操作方法,非常的必要!
1. 字符串的切片 []
字符串的切片是最常见的字符串操作,必须要掌握;
#切片语法 str_1='123456789' print(str_1[1:5]) # 输出为:2345 print(str_1[::]) #全部元素,输出为:123456789 print(str_1[1:8:2]) #输出为:2468 print(str_1[::-1]) #可以实现倒序,输出为:987654321 print(str_1[-5:-2]) #输出为:5672. 字符串的统计值 len(), count()
3. 查找字符串 find(),index()
查找字符串的方法有:index(), find(), rfind(), lfind(); index() 与 find() 的功能相同,不同点为:find()查找失败会返回-1,不会影响程序运行。一般用返回值来做判断,例如:find!=-1或者find>-1来作为判断条件。
#查找字符串 print('hello'.index('l')) # 输出2 print('hello'.find('l')) # 输出2 print('hello'.rfind('l')) # 输出3 print('hello'.rfind('a')) # 输出 -1 print('hello'.rfind('llo')) # 输出24. 字符串的替换 replace()
print('hello word'.replace('word', 'world')) #输出 hello world5. 连接字符串 + & join()
6. 分割字符串 split(),splitlines(),partition()
7. 包含运算 in & not in
a='hello world' print('hello' in a) # 输出:True print('hello' not in a) #输出:False8. 字符串大小写转换 upper(),lower(),title(),capitalize()
'hello world'.capitalize() # 第一个单词首字母大写,输出:'Hello world' 'hello world'.upper() #全部大写,输出:'HELLO WORLD' 'HELLO world'.lower() # 全部小写,输出:'hello world' 'hello world'.title() #每个单词的首字母大写,输出:'Hello World'9. 字符串的判断 islower(),endswith()等
'hello world'.startswith('e') # 输出: False 'hello world'.endswith('d') # 输出: True 'a'.isalpha() # 输出: False '23'.isdigit() # 输出: True '23'.isalnum() # 输出: True 'hello world'.isspace() # 输出: False 'hello world'.islower() # 字母是否全是小写 'hello world'.isupper() # 字母是否是大写 'hello world'.istitle() # 首字母是否大写10. 字符串与列表之间的转换 split(), join()
11. 字符串的对齐与居中 center(),ljust(),rjust()
# 对齐 三种剧中方式的参数均为:(width,fillchar) str_1.ljust(10) # 让字符串以制定长度显示,不足则以空格补齐; str_1.rjust(10,'#') ## 让字符串以制定长度显示,右对齐 str_1.center(20,'#') #让字符串以制定长度显示,居中12. 字符串的运算符 +,*,==,!=,<,>,=
13. 字符与编码的转换 ord(),chr(),encode(),decode()
# 字符与编码的转换 ord('这') #获得字符的编码值 chr(12456) #获得编码的字符 # GBK,UTF-8编码 print('你'.encode('GBK')) #十六进制:b'\xc4\xe3' 十进制:50403 二进制:11000100 11100011,一个字符占两个字节 print(b'\xc4\xe3'.decode('GBK')) # 输出:你! # UTF-8编码 print('你'.encode('utf8')) #十六进制:b'\xe4\xbd\xa0' 十进制:14990752 二进制:11100100 10111101 10100000,一个字符占三个字节 print(b'\xe4\xbd\xa0'.decode('utf8')) # 输出:你!14. 两种字符串占位符 (% & format{})
本文共计1819个文字,预计阅读时间需要8分钟。
Python基础学习之字符串操作+字符串是编程中最常见的类型,因此掌握好字符串的常用操作方法是非常必要的!
1.字符串的切片+[]+字符串的切片是最常见的字符串操作,必须熟练掌握;
Python基础学习之字符串操作
字符串是编程中最常遇到的类型,所以掌握好字符串的常用操作方法,非常的必要!
1. 字符串的切片 []
字符串的切片是最常见的字符串操作,必须要掌握;
#切片语法 str_1='123456789' print(str_1[1:5]) # 输出为:2345 print(str_1[::]) #全部元素,输出为:123456789 print(str_1[1:8:2]) #输出为:2468 print(str_1[::-1]) #可以实现倒序,输出为:987654321 print(str_1[-5:-2]) #输出为:5672. 字符串的统计值 len(), count()
3. 查找字符串 find(),index()
查找字符串的方法有:index(), find(), rfind(), lfind(); index() 与 find() 的功能相同,不同点为:find()查找失败会返回-1,不会影响程序运行。一般用返回值来做判断,例如:find!=-1或者find>-1来作为判断条件。
#查找字符串 print('hello'.index('l')) # 输出2 print('hello'.find('l')) # 输出2 print('hello'.rfind('l')) # 输出3 print('hello'.rfind('a')) # 输出 -1 print('hello'.rfind('llo')) # 输出2
