#-*-coding:utf-8-*-
'''
Created on 2015年10月20日
@author:
Administrator
'''
age="I am %d" % (10)
print age;
其中"I am %d"就是字符串模板,%为格式化符号,%后面添加了该占位符所代表的数据类型-整数10。
%s:采用str()格式输出字符串
%r:采用repr()格式输出字符串
%c:输出单个字符
%b:输出二进制数
%d:输出十进制数
%o:输出八进制数
%x:输出16进制数
其他一些类型,不再一一赘述。
为了输出的美观化,还经常会用到一些辅助指令:
*:定义数据的显示宽度
-:左对齐
+:在正数前面添加加号+符号
%:输出%
m.n:输出的数据最小位数为m,如果有浮点数,浮点数的精度为n
字符串内置函数
capitalize():Return a copy of the string S with only its first character capitalized.
将字符串首字符改写为大写。
center():Return S centered in a string of length width. Padding is done using the specified fill character (default is a space)。 语法:center(width[,fillchar]);将字符串在width的宽度中居中,并将空闲位置使用fillchar(默认空格)填充。
count():Return the number of non-overlapping occurrences of substring sub in string S[start:end].Optional arguments start and end are interpreted as in slice notation. 语法:S.count(sub[, start[, end]]) -> int;查找S[start:end]中sub字符串出现的次数。start默认0,end默认字符串长度。
decode()/encode():字符串根据编码格式编解码。
endwith():Return True if S ends with the specified suffix, False otherwise.With optional start, test S beginning at that position.With optional end, stop comparing S at that position.suffix can also be a tuple of strings to try. 语法:S.endswith(suffix[, start[, end]]) -> bool;判断S[start:end]是否以suffix结尾。
expandtabs():Return a copy of S where all tab characters are expanded using spaces.If tabsize is not given, a tab size of 8 characters is assumed. 语法:S.expandtabs([tabsize]) -> string;将tab符号转换为tablezie个空格,tablesize默认为8。
find():Return the lowest index in S where substring sub is found,such that sub is contained within S[start:end]. Optional arguments start and end are interpreted as in slice notation.Return -1 on failure. 语法:S.find(sub [,start [,end]]) -> int。返回S[start:end]中最早出现子串sub的索引值;如果没有改子串返回-1。
format():Return a formatted version of S, using substitutions from args and kwargs.The substitutions are identified by braces ('{' and '}'). 语法:S.format(*args, **kwargs) -> string。字符串的格式化函数。字符串中的参数使用{NUM}表示,{0}表示第一个参数,。。。
index():Like S.find() but raise ValueError when the substring is not found. 语法同find()方法,不同之处在于find()查找不到是返回-1;而index()会抛出异常。
isalnum():Return True if all characters in S are alphanumeric and there is at least one character in S, False otherwise. 语法:S.isalnum() -> bool;判断S中是否全是字母或者数字,如果是返回true;否则返回false。
isalpha():Return True if all characters in S are alphabetic and there is at least one character in S, False otherwise. 语法:语法同isalnum,与isalnum的不同在于isalpha只有当所有字符是字母时才返回true。
isdigit():所有字符是否全是数字。
islower():所有字符是否全是小写。
isspace():所有字符是否全是空格。
istitle():只有数字符为大写,其他字符不出现大写的情况下,返回true.
isupper():所有字符是否全是大写。
join():Return a string which is the concatenation of the strings in the iterable. The separator between elements is S. 语法:S.join(iterable) -> string;使用S将iterable中每个字符串联起来。
ljust():Return S left-justified in a string of length width. Padding is done using the specified fill character (default is a space). 语法:S.ljust(width[, fillchar]) -> string;字符串S在长度width的新串中左对齐,S右侧如有空闲位置使用 fillchar填充。
rpartition():Search for the separator sep in S, starting at the end of S, and return the part before it, the separator itself, and the part after it. If the separator is not found, return two empty strings and S. 语法:S.rpartition(sep) -> (head, sep, tail)。如果S.find(sep)!=-1,则返回由字符串sep前面部分,sep本身以及sep后面部分组成的list;否则的话前两部分都是空字符串。
split():Return a list of the words in the string S, using sep as the delimiter string. If maxsplit is given, at most maxsplit splits are done. If sep is not specified or is None, any whitespace string is a separator and empty strings are removed from the result. 语法:S.split([sep [,maxsplit]]) -> list of strings。如果S.find(sep)!=-1,则将S使用sep进行分割成包含maxsplit个元素的list;否则返回空。
splitlines(): Return a list of the lines in S, breaking at line boundaries.Line breaks are not included in the resulting list unless keepends is given and true. 语法:S.splitlines(keepends=False) -> list of strings。按照行进行分割,返回一个包含各行作为元素的列表;keepends控制结束符是否添加到各行元素中。
strip(): Return a copy of the string S with leading and trailing whitespace removed.If chars is given and not None, remove characters in chars instead. If chars is unicode, S will be converted to unicode before stripping。 语法:S.strip([chars]) -> string or unicode。移除字符串S的头尾指定字符chars或者空格。
strip(): Return a copy of the string S with leading and trailing whitespace removed.If chars is given and not None, remove characters in chars instead. If chars is unicode, S will be converted to unicode before stripping。 语法:S.strip([chars]) -> string or unicode。移除字符串S的头尾指定字符chars或者空格。
swapcase():字符串中所有的大小写互换。
title(): Return a titlecased version of S, i.e. words start with uppercase characters, all remaining cased characters have lowercase. 语法:将字符串S的每个单词(以空格分割)的首字母(第一个出现的字母,不包含数字)转换为大写,其他所有字符小写。
translate():字根据参数table中给定的表转换字符串中的字符。
upper():所有字符转为大写。
zfill(): Pad a numeric string S with zeros on the left, to fill a field of the specified width. The string S is never truncated. 语法S.zfill(width) -> string;字符串S右对齐,左侧的所有空附使用0填充。