Python字符串操作方法(函数)

1、 string capitalize()

将字符串的第一个字母大写

# Python字符串操作方法

# 1\ string capitalize() 将字符串的第一个字母大写
str1 = 'study python'
print(str1)
print(str1.capitalize())
study python
Study python

2、 string count(sub[start[,end]])

统计字符串中某一子字符串从start位置开始到end位置为止出现的个数

# 2\ string count(sub[start[,end]]) 统计字符串中某一子字符串从start位置开始到end位置为止出现的个数
info1 = """The first step is always the hardest."""

a = info1.count('a')
b = info1.count('b')
c = info1.count('c')

print(a, b, c)
number_list = [a, b, c]
print(number_list)
print('在列表在,最大的数值:', max(number_list))
3 0 0
[3, 0, 0]
在列表在,最大的数值: 3

3、string.find(sub[start[,end]])

返回某一子字符出现的起始位置,无则返回-1

# 3\string.find(sub[start[,end]]) 返回某一子字符出现的起始位置,无则返回-1
info2 = """This is my  Python note"""
T = info2.find('T')
c = info2.find('c')
print(T)
# T 在info列表是里第一个出现的,所以我们得到 T 的起始位置为:0
print(c)
# 在我们的info列表里没有出现c的,所以输出结果为:-1
0
-1

4、string.isalnum()

检测字符串是否包含0到9、a到z、A到Z

# 4\string.isalnum() 检测字符串是否包含0~9、a~z、A~Z
str2 = "Python"
y = str2.isalnum()
print(y)
# str2字符串(Python)中包含0~9、a~z、A~Z,所以输出为:True
str3 = " "
b = str3.isalnum()
print(b)
# str3字符串(” “)是空的,没有包含0~9、a~z、A~Z,所以输出为:False
True
False

5、string.isalpha()

检测字符串是否包含a到z、A到Z

# 5\string.isalpha() 检测字符串是否包含a~z、A~Z
str4 = "Python"
a = str4.isalpha()
print(a)
# str4字符串(Python)中包含a~z、A~Z,所以输出为:True
str5 = "1234"
a = str5.isalpha()
print(a)
# str5字符串(1234)中没有包含a~z、A~Z,全是数字,所以输出为:True
True
False

6、string.isdigit()

检测字符串中是否包含0~9

# 6\string.isdigit() 检测字符串中是否包含0~9
str4 = "Python"
b = str4.isdigit()
print(b)
str5 = "1234"
# str4字符串(Python)中包含0~9,所以输出为:True
b = str5.isdigit()
print(b)
# str5字符串(1234)中没有包含0~9,全是字母组成,所以输出为:True
False
True
## 7、string.islow() 

> 检测字符串是否均为小写字母``

```python
a = "python"
print(a.islow)
b = "Python"
print(b.islow)
True
False

8、string.isspace()

检测字符串是否均为空白字符
一些常见的空白字符是\ t,\ n,\ r,显然还有空白本身。

s = '   '
print(s.isspace())
 
s = '\t\n\r\t '
print(s.isspace())
 
s = '\u0009\t\u200a \u3000'
print(s.isspace())
True
True
True

9、string.istitle()

检测字符串的单词是否均为首字母大写

如果字符串是以标题格式开头的字符串,并且至少有一个字符,则返回 True,否则返回 False。

str = "This Is String Example...Wow!!!"
print (str.istitle())

 str = "This is string example....wow!!!"
print (str.istitle())
 
str = "This iS string example....wow!!!"
print (str.istitle())
True
False
False

10、string.issupper()

检测字符串中是否均为大写字母

str = "I LOVE YOU!"
print (str.istitle()

str = "I love you!"
print (str.istitle()
True
False

11、string.join(iterable)

连接字符串

# 11、string.join(iterable) 连接字符串
lag = "Python"
print(lag)
print('G'.join(lag))
Python
PGyGtGhGoGn

12、string.lower()

将字符串中的字母全部转换为小写

str = "YOU ARE A GOOD BOY!"
print(str)
print(str.lower())
YOU ARE A GOOD BOY!
you are a good boy!

13、string.split(sep=None)

分割字符串,默认用空格分割

# 13\string.split(sep=None) 分割字符串,默认用空格分割

str6 = 'I am a student'
print(str6)
info3 = str6.split(sep=' ')  # "sep=''" 表示以空格作为分割符,将字符串分割为一个列表
print(info3)
I am a student
['I', 'am', 'a', 'student']

14、string.swapcase()

将字符串中的大写字母转换为小写,将小写字母转换为大写

str = "I love Python!"
print(str)
print(str.swapcase())
I love Python!
i LOVE pYTHON!

15、string.title()

将字符串中单词的首字母大写

# 15\string.title() 将字符串中单词的首字母大写
print(str6.title())
I Am A Student

16、string.upper()

将字符串中的全部字母大写

# 16\string.upper() 将字符串中的全部字母大写
print(str6.upper())
I AM A STUDENT

17、len(string)

返回字符串的长度

# 17\len(string) 返回字符串的长度
print(len(str6))

18、string.strip([chars])

去除字符串首尾的空格、\n、\r、\t,如果指定,则去除首尾指定的字母

下面的英文说明是官方给出:

string.strip(s[, chars])

Return a copy of the string with leading and trailing characters removed. If chars is omitted or None, whitespace characters are removed. If given and not None, chars must be a string; the characters in the string will be stripped from the both ends of the string this method is called on.

Changed in version 2.2.3: The chars parameter was added. The chars parameter cannot be passed in earlier 2.2 versions.

下面例子中字符以tab抬头,以空格结尾。

line='    hello happybks! '
 
print '*'+line.strip()+'*'
print '*'+line.strip(' ')+'*'
print '*'+line.strip('    ')+'*'
print '*'+line.strip('h')+'*'
*hello happybks!*
*hello happybks!*
*hello happybks!*
*    hello happybks! *

可以发现不传参数,则会把字符串开头和结尾的空格、tab全部删除,中间的空格和tab不会

传空格或者tab参数,子串传仍然会把字符串开头和结尾的无论空格还是tab都一并删除

当传入的参数是其他参数时,字符串开头结尾不是该参数字符串,则没有任何效果

但是如果字符串的开头和结尾是其他字符串,并且传入的参数也是这个字符串,那么会将字符串开头和结尾的参数串全部清掉,无论有多少个。但是区分大小写。

例如,下面的例子:

line2='haaaaahhaaaaaaahHhhh'
print '*'+line2.strip('h')+'*'
*aaaaahhaaaaaaahH*