- 字符处理
- 字符截取
-
#通过索引访问字符 mystr="my string" mystr[0] # 'm' mystr[-2] #'n' #通过切片方式访问字符串的一部分 mystr[1:4] #'y s' mystr[3:] #'string' mystr[-3:] #'ing' #通过步长的方法获取 mystr[:3:-1] #'gnirt' mystr[1::2]#'ysrn' #循环遍历 for c in mystr: #乘法对字符串多次重复 'xo'*3 #xoxoxo #使用splitlines 分隔多个单行字符串 list=one_large_string.splitlines() #获取集合交集 import sets magic_chars=sets.Set('abcabcasds') po_chars=sets.Set('supoercalixaasdseasgoogle asdrf') print ''.join(magic_chars&po_chars) #集合的交集
#判断对象是否是字符串
isinstance(obj,basestring)
#字符串对齐
print '|','hej'.ljust(20),'|','hej'.rjust(20),'|','hej'.center(20),'|' - xx
- xx
转载于:https://www.cnblogs.com/tben/p/10895047.html