Python基础(String函数用法)

title()

作用:将字符串中所有英文单词的首字母大写

eg:

>>> test1 = ' my  name is Alex'
>>> print(test1.title()) 

 My  Name Is Alex

strip()

作用:删除字符串中开头和结尾的所有特殊符“\n,\r”

eg:

>>> test = "\n \n \n hello honey \n xixi \n nishi \n djaoifa \n"

>>> print(test.strip())

hello honey 
 xixi 
 nishi 
 djaoifa

lower()

作用:将字符串中所有英文单词转换为小写

eg:

>>> test1 = 'my  Name iS Alex'
>>> print(test1.lower()) 

  my name is alex

upper() 

作用:将字符串中的所有英文单词转换为大写

eg:

>>> test1 = ' My  Name iS Alex'
>>> print(test1.upper()) 

 MY NAME IS ALEX

center()

作用:将字符串放在中间左右为指定字符以及数量

eg:

>>> test1 = ' i  Name iS Alex'
>>> print(test1.center(40,"-"))

 ------------ i Name iS Alex------------

ljust()

作用:左对齐,不够的可以使用其他补充

eg:

>>> test1 = ' i  Name iS Alex'
>>> test2 = 'dadaddada'
>>> print(test1.ljust(40,"-"))
>>> print(test1.ljust(40))
>>> print(test2.ljust(40,"*"))

 i  Name iS Alex------------------------
 i  Name iS Alex                        
dadaddada*******************************

rjust()

作用:右对齐,不够的可以使用其他补充

eg: 

>>> test1 = ' i  Name iS Alex'
>>> test2 = 'dadaddada'
>>> print(test1.rjust(40,"-"))
>>> print(test1.rjust(40))
>>> print(test2.rjust(40,"*"))

  ---------------------------- i  Name iS Alex
                                     i  Name iS Alex
*******************************dadaddada

count()

作用:统计字符出现的次数

eg:

>>> test1 = 'this is test'
>>> print(test1.count("t"))
>>> print(test1.count("t",0,10))

3
2

encode()

作用:解决解码问题

eg:

>>> test1 = 'this is test'
>>> test2 = '这是一个测试'
>>> print(test1.encode(encoding='utf8',errors='sreict'))
>>> print(test2.encode(encoding='utf8',errors='sreict'))

b'this is test'
b'\xe8\xbf\x99\xe6\x98\xaf\xe4\xb8\x80\xe4\xb8\xaa\xe6\xb5\x8b\xe8\xaf\x95'

decode()

作用:解决字符编码问题

eg:

>>> test1 = 'this is test'
>>> test2 = '这是一个测试'
>>> str1 = test1.encode(encoding='utf8',errors='sreict')
>>> str2 = test2.encode(encoding='utf8',errors='sreict')
>>> print(str1.decode(encoding='utf8',errors='sreict'))
>>> print(str2.decode(encoding='utf8',errors='sreict'))

this is test
这是一个测试

find()

作用:返回找到字符串的索引位置

相反方向查找"rfind()"

eg:

>>> test1 = 'this is test'
>>> print(test1.find("i"))
>>> print(test1.find("i",4))
>>> print(test1.find("0"))

2
5
-1

format()

作用:返回一个格式化字符串

eg:

>>> test1 = '{first} this  is {number} test'
>>> print(test1.format(first="teststring",number="python"))

teststring this  is python test

endswith()

作用:判断字符串是否以指定字符结尾

eg:

>>> test1 = 'this  is test'
>>> print(test1.endswith("python"))
>>> print(test1.endswith("test"))

False
True

startswith()

作用:判断字符串是否以指定字符开头

 eg:

>>> test1 = 'this  is test'
>>> print(test1.startswith("python"))
>>> print(test1.startswith("this"))

False
True

isalnum()

作用:判断字符串是否以数字和字母组成

eg:

>>> test1 = 'thisistest123123'
>>> test2 = 'this is test 123 #$#$#$'
>>> print(test1.isalnum())
>>> print(test2.isalnum())

True
False

isalpha()

作用:检测字符串是否只由字母组成

eg:

>>> test1 = 'thisistest123123'
>>> test2 = 'thisistest'
>>> print(test1.isalpha())
>>> print(test2.isalpha())

False
True

isdigit()

作用:检测字符串是否只由数字组成

eg:

>>> test1 = 'thisistest123123'
>>> test2 = '123412312341'
>>> print(test1.isdigit())
>>> print(test2.isdigit())

False
True

islower()

作用:是否字母全是小写

eg:

>>> test1 = 'thisistest'
>>> test2 = 'Tasdasd123'
>>> print(test1.islower())
>>> print(test2.islower())

True
False

isupper() 

作用:是否字母全是大写

eg:

>>> test1 = 'thisistest'
>>> test2 = 'THISAHHA123'
>>> print(test1.isupper())
>>> print(test2.isupper())

False
True

ispace()

作用:检测是否只由空格构成

eg:

>>> test1 = '        '
>>> test2 = 'THISAHHA123'
>>> print(test1.isspace())
>>> print(test2.isspace())

True
False

partition()

作用:以指定字符进行分割,返回一个元组

从右往左“rpartition()”

eg:

>>> str = "https://www.baidu.com/"
>>> print(str.rpartition("://"))
>>> print(str.rpartition(","))  #字符串str中不存在sep",",返回了两个空字符串

('https', '://', 'www.baidu.com/')
('', '', 'https://www.baidu.com/')

split()

作用:以指定字符作为拆分符对字符串进行拆分返回一个列表

相反“rsplit()”

eg:

>>> str1 = "https://www.baidu.com/"
>>> str2 = "i do not love python"
>>> print(str1.split("."))
>>> print(str2.split())  #默认不写即为空格

['https://www', 'baidu', 'com/']
['i', 'do', 'not', 'love', 'python']

splitlines()

作用:以\n和\r做为分割符对字符串进行分割,“True”参数会显示符号

eg: 

>>> str1 = """i
love
python
"""
>>> print(str1.splitlines())
>>> print(str1.splitlines(True))

['i', 'love', 'python']
['i\n', 'love\n', 'python\n']

join()

作用:将字符串作为中数输入到列表中的每一个元素与元素之间,形成新的一串字符串

>>> list1 = ["abc","123","test","aadadad"]
>>> str1 = "*python*"
>>> print(str1.join(list1))

abc*python*123*python*test*python*aadadad 

replace()

作用:将字符串中的指定字符替换成新的字符

>>> str1 = "my my happ adasdmy test"
>>> print(str1.replace("my","#python#"))
>>> print(str1.replace("my","#python#",1))              #可以指定替换次数

#python# #python# happ adasd#python# test
#python# my happ adasdmy test

发布了65 篇原创文章 · 获赞 16 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_41674452/article/details/104051285
今日推荐