Python 字符串
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | #双引号spam="that is Alice's cat"#转义字符spam="say hi to bob\'s mother"#原始字符串 在字符串开始的引号之前加上r 使它成为原始字符串。"原始字符串"完全忽略所有的转移字符。print(r"That is Carol\'s cat")#三重尹行的多行字符串print('''dear aliceEve 's cat has been arrested for catnappting,cat burglary,and extortion.Sincerely''')#字符串的下标和切片 字符串可以想列表一样使用下标和切片spam='Hello Word!'print(spam[0])print(spam[0:6])#字符串的in和not in 操作符print('hello' in 'hello word')print('hello' not in 'hello word')#upper() 把字符串转换成大写spam="Hello Word"print(spam.upper())#lower() 把字符串转换成小写print(spam.lower())#isupper()判断字符串是否都是大写spam="HELLO"print(spam.isupper())#islower()判断字符串是否都是小写spam="hello"print(spam.islower())#isX字符串方法"""isalpha() 如果字符串只包含字母,并且非空isalnum() 如果字符串只包含字母和数字,并且非空isdecimal() 如果字符串只包含数字字符,并且非空isspace() 如果字符串只包含空格、制表符和换行,并且非空istitle()如果字符串仅包含以大写字母开通、后面都是小写字母的单词"""#startswith() 判断一个字符串是否是已这个字符串开头print("hello word".startswith("hello"))#endswith()判断一个字符串是否已这个字符串结尾print("hello word".endswith("word"))#join() join方法在一个字符上调用,参数是一个字符串列表,返回一个字符串print("abc".join(["my","name","is","simon"]))#split() split()方法做的事情正好相反,它针对一个字符串调用,返回衣服字符串列表。在交互环境中输入以下代码:print("My name is Simon".split())#rjust() 在字符串前面插入print('hello'.rjust(10))#ljust() 在字符串后面插入print('hello'.ljust(10))#center() 让文本居中print('hello'.center(10))#strip()print(' hello '.strip())#tstrip()print(' hello'.lstrip())#lstrip()print('hello '.rstrip())spam='SpamSpamBaconSpamEggsSpamSpam'print(spam.strip('ampS'))import pyperclippyperclip.copy('Hello word!')pyperclip.paste() |

浙公网安备 33010602011771号