06操纵字符串

转译字符串使用\可以直接打出‘和“。

Escape character Prints as
\’ ‘
\” “
\t 使用tab键的意思
\n 换行的意思
\ \

raw string 字符串

如果将r打在‘’之前,代表该字符串中所有的转译符号都是无效的。
print(r'That is Carol's cat.')
That is Carol's cat.

字符串方法和pyperclib模块

upper(),lower(),isupper(),islower
upper()和lower()分别使原来的字符串大小写发生转变,例如:
spam = 'Hello world!'
spam = spam.upper()
spam
'HELLO WORLD!'
spam = spam.lower()
spam
'hello world!'

isupper和islower用来判断该字符串是否为大写或者小写,例如:
spam = 'Hello world!'
spam.islower()
False
spam.isupper()
False
'HELLO'.isupper()
True

is开头的字符串方法
isalpha()——如果字符串只包含字母且不为空则为Ture;
isalnum()——如果字符串只含有字母和数字且不为空则为Ture;
isdecimal()——如果字符串只含有数字且不为空则为Ture;
isspace()——如果字符串只有空格,tab和换行,且不为空则为Ture;
istitle()——如果里面每句话的第一个字母是大写的,后面跟的都是小写则为Ture。

startswith()和endswith()
如果该字符串是以()中的字符为开始(或为结尾)则为Ture。
'Hello world!'.startswith('Hello')
True
'Hello world!'.endswith('world!')
True
'abc123'.startswith('abcdef')
False
'Hello world!'.endswith('Hello world!')
True

join()和split()
join()可以将列表中的字符串串联成同一个字符串。join前面用’’起来的东西就是用来连接字符串的内容。例如:
', '.join(['cats', 'rats', 'bats'])
'cats, rats, bats'
' '.join(['My', 'name', 'is', 'Simon'])
'My name is Simon'

split()和list()刚好相反,他将字符串通过’’里的内容拆分开来,例如:
'My name is Simon'.split( )
['My', 'name', 'is', 'Simon']

rjust()和ljust()
指向右(向左)调整多少空格的字符串。例如:
'Hello'.rjust(10)
' Hello'
'Hello'.rjust(20)
' Hello'
'Hello World'.rjust(20)
' Hello World'
'Hello'.ljust(10)
'Hello '

你可能感兴趣的:(06操纵字符串)