码迷,mamicode.com
首页 > 编程语言 > 详细

[Python] 字符串操作

时间:2020-05-01 20:44:13      阅读:65      评论:0      收藏:0      [点我收藏+]

标签:highlight   出现   bsp   isa   RoCE   min   输出   转换   pre   

字符串操作


以下字符串可以  变量.函数()    也可以   "字符串".函数()

 

capitalize()


首字母大写,其余转换为小写

print("test TEST".capitalize())

  输出

C:\Users\Administrator\AppData\Local\Programs\Python\Python37\python.exe D:/Python/Day02/test.py
Test test

Process finished with exit code 0

 count()


 count("str"),统计字符串出现的次数,跟列表的用法一样

print("test test".count("t"))

  输出

C:\Users\Administrator\AppData\Local\Programs\Python\Python37\python.exe D:/Python/Day02/test.py
4

Process finished with exit code 0

 center()


center(number,"str") 让内容显示在中间,使内容两侧填充字符串,默认是空格

 

print("test".center(50,"-"))

  输出

C:\Users\Administrator\AppData\Local\Programs\Python\Python37\python.exe D:/Python/Day02/test.py
-----------------------test-----------------------

Process finished with exit code 0

 

endswith()


 endswith("str"),判断字符串结尾是否包含 “str“,是则True 否则False

print("i am shuaige".endswith("ge"))

  输出

C:\Users\Administrator\AppData\Local\Programs\Python\Python37\python.exe D:/Python/Day02/test.py
True

Process finished with exit code 0

 find()


find("str") 查找字符串并返回索引值,如果不存在则返回 -1

print("i am shuaige".find("am"))

  输出

C:\Users\Administrator\AppData\Local\Programs\Python\Python37\python.exe D:/Python/Day02/test.py
2

Process finished with exit code 0

 

format_map()


和format用法一样,字符格式化。只是格式不一样

print("my name is {name} i am {age} years old".format_map({"name":"DaHua","age":10}))

  输出

C:\Users\Administrator\AppData\Local\Programs\Python\Python37\python.exe D:/Python/Day02/test.py
my name is DaHua i am 10 years old

Process finished with exit code 0

isalnum()


 

str.isalnum()  检查字符串是否仅为字母、数字,或者为字母数字混合,是则返回 True,否则返回 False

print("test123".isalnum())

  输出

C:\Users\Administrator\AppData\Local\Programs\Python\Python37\python.exe D:/Python/Day02/test.py
True

Process finished with exit code 0

 

isalpha()


 

str.alpha() 检查字符串是否为字母,是则返回True,否则返回False

print("abc".isalpha())

  输出

C:\Users\Administrator\AppData\Local\Programs\Python\Python37\python.exe D:/Python/Day02/test.py
True

Process finished with exit code 0

 

isdecimal()


 

str.isdecimal() 检查字符串是否只为十六进制

print("000000".isdecimal())

  输出

 

[Python] 字符串操作

标签:highlight   出现   bsp   isa   RoCE   min   输出   转换   pre   

原文地址:https://www.cnblogs.com/ITkeyan/p/12813056.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!