python字符串常用方法

#1、strip()去掉空格(字符串首、尾空格)、lstrip()去掉左侧空格、rstrip()去掉右侧空格
print(' abc '.lstrip())
#>>abc
print(' abc '.rstrip())
#>> abc
print(' abc '.strip())
#>>abc

#2、split 切割,splitlines按照换行符分割 注:结果为列表
print('a|b|c'.split('|'))
#>>['a', 'b', 'c']
print('1+2+3 1+2+3+4'.splitlines()) # 按照换行符分割
#>>['1+2+3', '1+2+3+4']

split与os.path.split()对比

Split 字符串

拆分字符串。返回字符串列表。

1、默认情况下,使用空格作为分隔符,则分隔后,空串会自动忽略

s = 'love    python'
print(s.split())
#>>>>>['love', 'python']

2、若显示指定空格做分隔符,则不会忽略空串

s = 'love    python'
print(s.split(' '))
#>>>>>['love', 'xa0', 'xa0python']

3、指定分隔次数

s = 'www.pku.edu.cn'
print(s.split('.',0))
#>>>>>['www.pku.edu.cn']
print(s.split('.',1))
#>>>>>['www', 'pku.edu.cn']
print(s.split('.',-1)) #尽可能多的分隔,与不加num参数相同
#>>>>>['www', 'pku', 'edu', 'cn']

4、分隔后直接存入变量

(分隔1次,并把分隔后的2个字符串存放在s1和s2中)

s1,s2=s.split('.',1)
print(s1)
print(s2)
#>>>>>www
#>>>>>pku.edu.cn

os.path.split()

返回元组类型

将文件名和路径分割开

import os
t=os.path.split('C:/soft/python/test.py') #将文件名和路径分割开
print(t)
#>>>>>('C:/soft/python', 'test.py')





#3、upper() 所有字母大写,lowwer()所有字母小写,swapcase() 大小写反转
print('abc'.upper())
#>>ABC
print('ABC'.lower())
#>>abc
print('Abc'.swapcase())
#>>aBC

#4、replace(‘原字符’,‘新字符’)
print('hello cat'.replace('hello','hi'))
#>>hi cat

#5、找位置序列号
# index() 按照指定字符返回索引号 注:如果不存在将报错
# find() 按照指定字符返回索引号 注:如果不存在不报错,返回-1
print('abc'.index('a'))
#>>0
print('abc'.find('t'))
#>>-1

#6、count() 查找某个字符总个数
print('abcattv'.count('t'))
#>>2

#7、切片
# 注意形式':' 结果左闭右开
print('abcdef'[0:5])
#>>abcde

#8、填充0 zfill(n) 在字符串前加0,字符总数n
print('abc'.zfill(5))
#>>00abc

#9、放中间位置 (字符总数,补充的字符)
print('abc'.center(20,'t'))
#>>ttttttttabcttttttttt

#10、格式化
# a:用%s占位 注:不带'.'
print('a%sc'%('ttt'))
#>>atttc
# b:format 用{}占位
print('a{}c'.format('b'))
#>>abc
print('{}+{}={}'.format(1, 2, 1 + 2))
#>>1+2=3
#c:format_map 用{}占位 format_map后接字典形式
print('a{t1}c{t2}'.format_map({'t1':'b','t2':'d'}))
#>>abcd
#d:f用{}占位 -- python3.6以上支持
print(f"a{'b'}c")
#>>abc


#11、字符串连接 join内容为字符串、列表形式
print('123'.join('deg'))
#>>d123e123g
print('+'.join('abc'))
#>>a+b+c
#12、expandtabs(通常可用于表格格式的输出),返回字符串中的 tab 符号('	')转为空格后生成的新字符串
info ="name age email lily 22 123@qq.com james 33 456@qq.com"
print(info.expandtabs(10))
#>>
'''
name age email
lily 22 123@qq.com
james 33 456@qq.com
'''
#13、import string
import string
#0-9之间整数集
string.digits
# 特殊字符集
string.punctuation
# 小写字母集
string.ascii_lowercase
# 大写字母集
string.ascii_uppercase


#------------------返回布尔类型---------------------------

#1、是否以某字符开头、结尾----返回true与false
print('ttt'.startswith('t'))
#>>True
print('ttt'.endswith('t'))
#>>True

#2、是否是数字与字母
# 是否是数字
print('3335'.isdigit())
#>>True
#是否全部是字母或者数字
print('aaa11'.isalnum())
#>>True
#是否全部是字母
print('1'.isalpha())
#>>False

#3、是否是一个合法的变量名
print('aa'.isidentifier()) # 是否是一个合法的变量名
#4、判断是否是大小写
print('aa'.islower()) # 是否是小写字母
print('AA'.isupper()) # 是否是大写字母

#5、判断首字母是否大写
print('Toadrunner Book'.istitle()) # 是不是一个标题,判断首字母是否大写
#>>True
print('Toadrunner book'.istitle())
#>>False
原文地址:https://www.cnblogs.com/wenchengqingfeng/p/9213706.html