常见又实用的Python字符串操作小技巧,python字符串小技巧,print "Fill


print "Fill an empty string:"empty_string = ""for k in range(65, 91):  empty_string += chr(k)print empty_stringprint "\nOriginal string (California humor):"s = "She has 8 body piercings and none are visible"print s#www.iplaypy.comprint "\nSeparate string at any whitespace to a list of words:"sL = []sL = s.split()print sLprint "\nShow item 4 on the list (lists are zero based):"print sL[4]# iterate through the list and also show item numbersprint "\nIterate (walk) through the list:"for k in range (len(sL)):    print k, sL[k]# print words and lengthprint "\nAgain this time show words and ( length ):"for word in sL:    print word, " (", len(word), ")"# append one more wordprint "\nAdd one more word to the end:"sL.append("yet!")print sLprint "\nJoin the list of words to form a string again:"# single space = " " as a delimiterprint " ".join(sL) + '\n'# a string can be enclosed in " or ' so if you want to# use " as part of the string, enclose it with 's2 = '"How to Serve Your Fellow Man"'s3 = "Cannibal's recipe book:"# left justify string s3 and pad with 2 spaces past its length# then concatenate ( use + ) with string s2print "Concatenate two strings, leftjustify and pad first string:"print s3.ljust(len(s3) + 2) + s2 + '\n's4 = "hippopotamus"print "full string  = ", s4print "spell/space it:"for char in s4:  print char,printprint "count the characters:"# create an empty dictionarycharCount = {}for char in s4:  charCount[char] = charCount.get(char, 0) + 1print charCountprint "full string  = ", s4print "first char   = ", s4[0]print "last char    = ", s4[-1]"""now for something completely different, slicing ...[starting-at-index : but-less-than-index [ : step]]start defaults to 0, end to len(sequence), step to 1"""print "first 2 char = ", s4[0:2]print "next 2 char  = ", s4[2:4]print "last 2 char  = ", s4[-2:]print "exclude first 3 char  = ", s4[3: ]print "exclude last 4 char   = ", s4[:-4]print "reverse the string    = ", s4[::-1]print "the whole word again  = ", s4# [start:end:step]print "spell skipping 2 char = ", s4[::2]print "concatenate 3 strings = ", s4 + s4 + s4# same resultprint "simply multiply by 3  = ", s4 * 3# prints 50 dashesprint '-' * 50printprint "Convert an integer or float to a string with repr() or str():"num1 = 3.14print "num1       =", num1print "repr(num1) = %s" % repr(num1)print "str(num1)  = %s" % str(num1)printstr1 = str(num1)print "Convert numeric string back to an integer or float with eval():"num2 = eval(str1)print "eval(str1)       =", eval(str1)print "type(eval(str1)) =", type(eval(str1))print "If you know the type, you can use int(str1) or float(str1)"printprint "An added bonus, function int() has a base option:"print "binary to decimal int('1111', 2)     =", int('1111', 2)print "hexadecimal to decimal int('FF', 16) =", int('FF', 16)printprint "One more bonus, eval() can evaluate a math expression string:"print "eval('3 * 4 + 2') =", eval('3 * 4 + 2')printprint "Comparing strings:"print "cmp('mouse', 'mouse') = ", cmp('mouse', 'mouse')print "cmp('mouse', 'louse') = ", cmp('mouse', 'louse')print "cmp('louse', 'mouse') = ", cmp('louse', 'mouse')print# make your own words, a little character fun ...str1 = 'Aack'       # 'Auck' might be a temptationprint "Replace A in %s with other letters:" % str1# go from B to Zfor b in range(66, 91):  ch = chr(b)  if ch == 'Q':     # special case Q, use Qu    ch = ch + 'u'  print str1.replace('A', ch)  print# how to create a multiline string ...print "This is a multiline string:"mlStr = """Noses are running.Feet are smelling.Park on driveway.Drive on parkwa3b8y.Recite at a play.Play at a recital."""print mlStrprint "Show the line that starts with Park:"# find Park index/positionpos1 = mlStr.find("Park")# find index of the end of that linepos2 = mlStr.find("\n", pos1)# slice the line from the stringline = mlStr[pos1:pos2]print lineprint# if you just want to see if a substring is there ...subStr = 'smell'if subStr in mlStr:    print "Found substring '%s'" % subStrprintprint "There are", mlStr.count('a'), "'a' in the multiline string."print "They are at index:"index = mlStr.find('a')  # find firstprint indexwhile index != -1:      # look for more    index = mlStr.find('a', index + 1)    if index > 0:        print indexprintprint "Extract a substring located between two given substrings, here the quotes:"def extract(text, sub1, sub2):    """extract a substring between two substrings sub1 and sub2 in text"""    return te3c48xt.split(sub1)[-1].split(sub2)[0]str3 = 'I bought the "Python Cookbook" and could not find one single recipe about cooking the slithery beast!'# notice that here beginning and trailing spaces can be included with the quotesstr4 = extract(str3, ' "', '" ')print str3print str4printprint "Line continuation (\) can also be used for long or multiline strings:"# combining three strings to one string# (ignores whitespace between strings)# (do not add a space right behind the \)str1 = "The alien wheezes, 'Darn, this is it.  I will die now! \n"\       "Tell my 2.4 million larvae that I esteem them ... \n"\       "Good-bye, truculent universe.'"print str1print# file names are strings, so let's find them# find all the .py files in the working folder (path = '')import os  # module needed for listdir()print "Add all the python files in the working folder to a list:"path = ''ext  = '.py'filelist = []for filename in os.listdir(path):  if filename.endswith(ext):    filelist.append(filename)# show the listprint filelistprint "\nPrint the file list one item on a line:"# new line as delimiterprint "\n".join(filelist)print# a function can have a documentation stringdef formatDollar(amount):  "formatDollar(amount) returns a string with the amount formatted to $ currency"  return "$%.2f" % amountprint "The function's documentation string:"print formatDollar.__doc__printprint "For example", 123.9 * 0.0725,"formatted to", formatDollar(123.9 * 0.0725)

编橙之家文章,

评论关闭