字符/字符串
python中的字符串(str)是list的一种。
返回字符的Unicode编码
ord(c)
Given a string representing one Unicode character, return an integer representing the Unicode code point of that character. For example, ord('a') returns the integer 97 and ord('\u2020') returns 8224. This is the inverse of chr().
字符串格式化
传统方式:
str = "%s, %d" % ("数字", 12)
功能更强大:
str = "{0}, {1}, {0}".format("数字", 12)
str = "{x}, {y}, {x}".format(x="数字", y=12)
注:字符串在中需要表达"{}"时,用"{{}}"
单词格式化
>>> "hello world".title()
'Hello World'
>>> "中国人民解放军".title()
'中国人民解放军'
>>> "PLA".title()
'Pla'
>>> "they're bill's friends from the UK".title()
"They'Re Bill'S Friends From The Uk"
>>> import string
>>> string.capwords("they're bill's friends from the UK")
"They're Bill's Friends From The Uk"
判断字符串是否由多个字符串重复组成
[
'0045662100456621004566210045662100456621', # '00456621'
'0072992700729927007299270072992700729927', # '00729927'
'001443001443001443001443001443001443001443', # '001443'
'037037037037037037037037037037037037037037037', # '037'
'047619047619047619047619047619047619047619', # '047619'
'002457002457002457002457002457002457002457', # '002457'
'001221001221001221001221001221001221001221', # '001221'
'001230012300123001230012300123001230012300123', # '00123'
'0013947001394700139470013947001394700139470013947', # '0013947'
'001001001001001001001001001001001001001001001001001', # '001'
'001406469760900140646976090014064697609', # '0014064697609'
]
def principal_period(s):
i = (s+s).find(s, 1, -1)
return None if i == -1 else s[:i]
字符串的index
从字符串看 list 的 index, 以下来自Python帮助文档
/*******
+---+---+---+---+---+
| H | e | l | p | A |
+---+---+---+---+---+
0 1 2 3 4 5
-5 -4 -3 -2 -1
************************/
随机生成字符串(字符+数字)
原文
This Stack Overflow quesion is the current top Google result for "random string Python". The current top answer is:
import random
import string
''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(N))
This is an excellent method, but the PRNG in random is not cryptographically secure. I assume many people researching this question will want to generate random strings for encryption or passwords. You can do this securely by making a small change in the above code:
''.join(random.SystemRandom().choice(string.ascii_uppercase + string.digits) for _ in range(N))
Using random.SystemRandom() instead of just random uses /dev/urandom on *nix machines and CryptGenRandom() in Windows. These are cryptographically secure PRNGs. Using random.choice instead of random.SystemRandom().choice in an application that requires a secure PRNG could be potentially devastating, and given the popularity of this question, I bet that mistake has been made many times already.
移除/替换字符串中的多个字符
Python3.x 参见:translate
>>> chars_to_remove = ('.', '!', '?')
>>> subj = 'A.B!C?'
>>> dd = {ord(c):None for c in chars_to_remove}
>>> subj.translate(dd)
'ABC'
...
>>> tt = str.maketrans("", "", ".!?")
>>> subj.translate(t)
'ABC'
用“whitespace”分割字符串
str.split()方法不传参时用“whitespace”分割字符串:
>>> "many fancy word \nhello \thi".split()
['many', 'fancy', 'word', 'hello', 'hi']