Python10:String字符串

字符串有很多方法,可以学完,但用不完,掌握常用的就可以,不常用可以找文档现学现用。

方法操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind

str = "this is beautiful girl"
print (str.capitalize())

输出:

This is beautiful girl

Process finished with exit code 0

解释:

str.capitalize():首字母大写。

方法操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind

str = "this is beautiful girl"
print (str.count("s"))

输出:

2

Process finished with exit code 0

解释:

str.count("s"):统计字母“s”出现的次数。

方法操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind

str = "this is beautiful girl"
print (str.center(50,"-"))

输出:

--------------this is beautiful girl--------------

Process finished with exit code 0

解释:

str.center(50,"-"):输出50个字符,文本占中间,不够用"-"补齐。

方法操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind

str = "this is beautiful girl"
print (str.endswith("rl"))

输出:

True

Process finished with exit code 0

解释:

str.endswith("rl"):判断是否以"rl"结尾

方法操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind

str = "this is beautiful girl"
print (str.expandtabs(tabsize=30))

输出:

this is                       beautiful girl

Process finished with exit code 0

解释:

expandtabs(tabsize=30):设置制表符的占位数。

方法操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind

str = "this is beautiful girl"
print (str.find("is"))

输出:

2

Process finished with exit code 0

解释:

str.find("is"):查找字符出现的位置。

方法操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind

str = "this is beautiful girl's name is {name}"
print (str.format(name="Rose"))

输出:

this is beautiful girl's name is Rose

Process finished with exit code 0

解释:

str.format(name="Rose"):字符串格式输出。

方法操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind

str = "this is beautiful girl's name is {name}"
print (str.format_map({"name":"Rose"}))

输出:

this is beautiful girl's name is Rose

Process finished with exit code 0

解释:

str.format_map({"name":"Rose"}):字符串格式输出。

方法操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind

print ("abc123".isalnum())

输出:

True

Process finished with exit code 0

解释:

"abc123".isalnum():判断是不是英文加阿拉伯数字。

方法操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind

print ("asdf".isalnum())

输出:

True

Process finished with exit code 0

解释:

"asdf".isalnum():判断是否是英文字符。

方法操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind

print ("123".isdecimal())

输出:

True

Process finished with exit code 0

解释:

"123".isdecimal():判断是否是十进制数。

方法操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind

print ("123".isdigit())

输出:

True

Process finished with exit code 0

解释:

"123".isdigit():判断是否是整数。

方法操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind

print ("123".isidentifier())

输出:

False

Process finished with exit code

解释:

"123".isidentifier():判断是否是一个合法的标识符。

方法操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind

print ("123".isnumeric())

输出:

True

Process finished with exit code 0

解释:

"123".isnumeric():判断是否是数值 。

方法操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind

print (" ".isspace())

输出:

True

Process finished with exit code 0

解释:

" ".isspace():判断是否是空格。

方法操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind

str = "this is beautiful girl"
print (str.istitle())

输出:

False

Process finished with exit code 0

解释:

print(name.istitle()) #判断是不是一个标题(每个首字母大写)。

方法操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind

str = "this is beautiful girl"
print (str.isprintable())

输出:

True

Process finished with exit code 0

解释:

str.isprintable():判断是否是可打印的。

方法操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind


print ("ABC".isupper())

输出:

True

Process finished with exit code 0

解释:

"ABC".isupper():判断是否是大写。

方法操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind

print("+".join(["one","two","three"]))

输出:

one+two+three

Process finished with exit code 0

解释:

"+".join(["one","two","three"]):“+”连接列表元素。

方法操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind

str = "this is beautiful girl"
print(str.ljust(50,"*"))

输出:

this is beautiful girl****************************

Process finished with exit code 0

解释:

str.ljust(50,"*" ):长50,不够右边补*。

方法操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind

str = "this is beautiful girl"
print(str.rjust(50,"*"))

输出:

****************************this is beautiful girl

Process finished with exit code 0

解释:

str.rust(50,"*" ):长50,不够左边补*。

方法操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind

print("ABC".lower())

输出:

abc

Process finished with exit code 0

解释:

"ABC".lower():变小写。

方法操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind

print("abc".upper())

输出:

ABC

Process finished with exit code 0

解释:

"abc".upper():变大写。

方法操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind

print("abc ".rstrip())
print("ABC")

输出:

abc

ABC

Process finished with exit code 0

解释:

"abc ".rstrip():去掉右边的回车。

方法操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind

print("abc")
print(" ABC".lstrip())

输出:

abc

ABC

Process finished with exit code 0

解释:

" ABC".lstrip():去掉左边的回车。

方法操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind

p = str.maketrans("abcdef","123456")
print("abcxyz".translate(p))

输出:

123xyz

Process finished with exit code 0

解释:

p = str.maketrans("abcdef","123456")
print("abcxyz".translate(p)):密码替换。

方法操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind

print("abclcbla".rfind("l"))

输出:

6

Process finished with exit code 0

解释:

"abclcbla".rfind("l"):返回字母l的位置,最右边的值的位置。

方法操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind

str = "this is beautiful is girl"
print(str.replace("is","was",1))

输出:

thwas is beautiful is girl

Process finished with exit code 0

解释:

str.replace("is","was",1):字符串替换,第三个参数可选,不写的时候默认为1。

方法操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind

str = "this is beautiful is girl"
print(str.split())

输出:

['this', 'is', 'beautiful', 'is', 'girl']

Process finished with exit code 0

解释:

str.split():把字符串按空格分格成列表。

方法操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind

str = "this is beautiful is girl"
print(str.split("is"))

输出:

['th', ' ', ' beautiful ', ' girl']

Process finished with exit code 0

解释:

print(name.split('l')) :把“is”当成分隔符将字符串分隔

方法操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind

str = "this is beaut iful is girl"
print(str.splitlines())

输出:

['this is beaut', 'iful is girl']

Process finished with exit code 0

解释:

str.splitlines():把换行当成分隔符。

方法操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind

str = "this is beautiful is girl"
print(str.swapcase())

输出:

THIS IS BEAUTIFUL IS GIRL

Process finished with exit code 0

解释:

str.swapcase():大写。

方法操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind

str = "this is beautiful is girl"
print(str.title())

输出:

This Is Beautiful Is Girl

Process finished with exit code 0

解释:

str.title():变成标题。

方法操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind

str = "this is beautiful is girl"
print(str.zfill(50))

输出:

0000000000000000000000000this is beautiful is girl

Process finished with exit code 0

解释:

str.zfill(50):字符串占位50不够的话用0补充。

原文地址:https://www.cnblogs.com/mclind/p/8762498.html