python字符串-方法

一、
1. upper()
作用:将字符串中字符转换为大写

In [17]: spam
Out[17]: 'hello,world'

In [18]: print(spam.upper())
HELLO,WORLD

2.lower()
作用:将字符串中字符转换为小写

In [19]: spam = spam.upper()

In [20]: spam
Out[20]: 'HELLO,WORLD'

In [21]: print(spam.lower())
hello,world

3.isupper()
作用:判断字符串中是否有大写字符

In [22]: spam
Out[22]: 'HELLO,WORLD'

In [23]: spam.isupper()
Out[23]: True

4.islower()
作用:判断字符串中是否有小写字符

In [24]: spam
Out[24]: 'HELLO,WORLD'

In [25]: spam.islower()
Out[25]: False

5.isalpha()
isalpha()返回 True,如果字符串只包含字母,并且非空

In [27]: spam
Out[27]: 'HELLO,WORLD'

In [28]: spam.isalpha()
Out[28]: False

6.isalnum()
isalnum()返回 True,如果字符串只包含字母和数字,并且非空

In [39]: mystring01 = '123hello'

In [40]: mystring01.isalnum()
Out[40]: True

7.isdecimal()
isdecimal()返回 True,如果字符串只包含数字字符,并且非空

In [46]: mystring01 = '123456'

In [47]: mystring01.isdecimal()
Out[47]: True

8.isspace()
isspace()返回 True,如果字符串只包含空格、制表符和换行,并且非空

In [53]: mystring01 = '
 '

In [54]: mystring01.isspace()
Out[54]: True

9.istitle()
istitle()返回True,如果字符串仅包含以大写字母开后面都是小写字母的单词

In [62]: mystring01 = 'Helloworld'

In [63]: mystring01.istitle()
Out[63]: True

10.startswith()
startswith()方法返回 True,如果它们所调用的字符串以该方法传入 的字符串开始

In [71]: 'hello world'.startswith('hello')
Out[71]: True

11.endswith()
endswith()方法返回 True,如果它们所调用的字符串以该方法传入 的字符串开结束

In [72]: 'hello world'.endswith('ld')
Out[72]: True

12.join()
拼接字符串列表

In [76]: '-'.join(['aaa','bbb','ccc'])
Out[76]: 'aaa-bbb-ccc'

13.split()
分解字符串

In [79]: 'aaa-bbb-ccc'.split("-")
Out[79]: ['aaa', 'bbb', 'ccc']

14.strip()
(1)删除两侧空白字符

In [86]: print(mystring01)
  hel lo , w o r ld

In [87]: print(mystring01.strip())
hel lo , w o r ld

(2)删除指定字符串

In [108]: print(mystring01)
  hel lo , w o r ld

In [109]: print(mystring01.strip(' hel lo'))
, w o r ld

15.lstrip()
删除左边空白字符

In [89]: print(mystring01)
  hel lo , w o r ld

In [90]: print(mystring01.lstrip())
hel lo , w o r ld

16.rstrip()
删除右边空白字符

In [92]: print(mystring01)
  hel lo , w o r ld

In [93]: print(mystring01.rstrip())
  hel lo , w o r ld

17.pyperclip模块
使用pyperclip模块总的copy和paste参数

import pyperclip
pyperclip.copy('Hello world!')
pyperclip.paste()





原文地址:https://www.cnblogs.com/dingkailinux/p/8461407.html