Fork me on GitHub

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 alice
Eve '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 pyperclip
pyperclip.copy('Hello word!')
pyperclip.paste()



posted @ 2017-02-24 00:51  大道化简  阅读(116)  评论(0)    收藏  举报