旧式字符串格式化
%操作符可以实现字符串格式化。它将左侧的参数作为类似sprintf()式的格式化字符串,而将右侧的代入,然后返回格式化后的字符串。
for example:
1 |
|
1 |
|
1 |
|
str.index(sub[,start[,end]])
Like find(),but raise ValueError when the substring is not found.
区别与find(),find的结果为False时返回-1,index()的查询值如果不在str中,则会报错。
1 |
|
str.isalnum()
Return true if all characters in the string are alphanumeric and there is at least one character,false otherwise. A character c is alphanumeric if one of the following returns True: c.isalpha(), c.isdecimal(), c.isdigit(), or c.isnumeric().
如果都是字母和数字时候,return True, else return False.
str.isalpha()
Return true if all characters in the string are alphabetic and there is at least one character, false otherwise.Alphabetic characters are those characters defined in the Unicode character database as “Letter”,ie.e., those with general category property being one of “Lm”,”Lt”, “Lu”,”Ll” or “Lo” .Note that this is different from the “Alphabetic” propertty defined in the Unicode Standard.
判断str是否都是数字
str.isdecimal()
Return true if all characters in the string are decimal characters and there is at least one character,false otherwise.Decimal characters are those from general catefory “Nd”. This category includes digit characters,and all characters that that can be used to form decimal-radix numbers,e.g. U+0660,ARABIC-INDIC DIGIT ZERO.
判断str是否是十进制数。
str.lstrip([chars])
Return a copy of the string with leading characters removed. The chars argument is a string specifying the set of characters to be removed. If omitted orNone
, the chars argument defaults to removing whitespace. The chars argument is not a prefix; rather, all combinations of its values are stripped:
' changxufeng'.lstrip()
\当argu为空是,返回结果为去掉str前面的空格。不影响原来str的值,只是一份copy
'changxufeng'.lstrip(chang)
\return ‘xufeng’
如果去掉首位的空格可以使用str.strip()
'changxufeng'.rstrip('xufeng')
\return ‘cha’
str.title()
Return a titlecased version of the string where words start with an uppercase character and the remaining characters are lowercase.
The algorithm uses a simple language-independent definition of a word as groups of consecutive letters. The definition works in many contexts but it means that apostrophes in contractions and possessives form word boundaries, which may not be the desired result:
string中首字母大写。
- 本文标题:old-string-formatting
- 本文作者:Roy
- 创建时间:2018-03-01 14:07:22
- 本文链接:https://www.yrzdm.com/2018/03/01/old-string-formatting/
- 版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!