Character width and precision:>>> from math import pi>>> '%10f '%pi # Field width ' 3.141593 ' >>> '%10.2f ' %pi #字段宽10, accuracy 2 ' 3.14 ' >>> '%.2f '%pi #精度2 ' 3.14 ' >>> '%.5s ' % ' guido van Rossum ' #指定获取字符串的个数 ' Guido ' >>> '%-10.2f ' %pi #左对齐 ' 3.14 ' >>> '%+10.2f ' %pi #右对齐 ' +3.14 ' Find method: Returns the leftmost index of the position where the string is located >>> data = ' With a moo-moo here, and a moo-moo there ' >>> data.find (' moo ') 7join method: The given string is stitched together according to the specified link symbol >>> dirs = [' usr ', ' bin ', ' env ']>>> '/'. Join (dirs) ' Usr/bin/envlower method: Convert uppercase letters to lowercase >>> user = ' Trondheim hammer dance ' >>> user.lower () ' Trondheim hammer dance ' Title method: Convert a string to a caption >>> "That's S all floks". Title () "That ' S all floks" replace method: replaces the specified string, replacing multiple characters >>> data = ' this is a test ' >>> data.replace (' is ', ' Jeff ') ' thjeff Jeff a testsplit method: Split string by specified delimiter >>> dirs = '/usr/sbin/env ' >>> dirs.split ('/') [', ' usr ', ' sbin ', ' env ']strip method: Remove the strings on both sides (excluding interior) spaces >>> ' why what who when how '. Strip () ' why What who when how ' Translate method: Just as with the Replace method, replace only a single character >>> from string Import maketrans >>> table = maketrans (' CS ', ' KZ ') >>> table[ 97:123] ' abkdefghijklmnopqrztuvwxyz ' >>> maketrans (', ') [97:123] ' abcdefghijklmnopqrstuvwxyz ' > >> ' this is an incredible test '. Translate (table) ' thiz iz an Inkredible tezt ' >>>&nbsP; ' This is an incredible test '. Translate (table, ' ') #第二个参数为要删除的字符 ' Thizizaninkredibletezt '
This article from "Trot Empty" blog, declined reprint!
Python Learning note string (str)