Python System Study Notes (2) --- string

Source: Internet
Author: User

Str = 'python String function' generate String variable str = 'python String function' String length acquisition: len (str) Example: print '% s length = % d' % (str, len (str) uppercase letters: str. upper () all lower case: str. lower () case-insensitive swap: str. swapcase () has upper-case letters and other lower-case letters: str. capital: str. title () print '% s lower = % s' % (str, str. lower () print '% s upper = % s' % (str, str. upper () print '% s swapcase = % s' % (str, str. swapcase () print '% s capitalize = % s' % (str, str. capitalize () print '% S title = % s' % (str, str. the title () is formatted to obtain a fixed length, right alignment, and the left side is not filled with spaces: str. ljust (width) gets a fixed length, which is left aligned. If the right side is not enough, use a space to fill in: str. ljust (width) gets a fixed length and is aligned in the middle. The two sides are not filled with spaces: str. ljust (width) gets a fixed length, right alignment, and print '% s ljust = % s' % (str, str. ljust (20) print '% s limit ust = % s' % (str, str. must ust (20) print '% s center = % s' % (str, str. center (20) print '% s zfill = % s' % (str, str. zfill (20) string search related searches specified string, no-1: str. find ('T') specified start position search: str. find ('T', start) specifies Start position and end position search: str. find ('T', start, end) from the right to find: str. how many specified strings are searched by rfind ('T'): str. all the methods above can be replaced by indexes in count ('T'). The difference is that if index is not found, an exception is thrown, while find returns-1 print '% s find nono = % d' % (str, str. find ('nono') print '% s find t = % d' % (str, str. find ('T') print '% s find t from % d = % d' % (str, 1, str. find ('T', 1) print '% s find t from % d to % d = % d' % (str, 1, 2, str. find ('T', 1, 2) # print '% s index nono' % (str, str. index ('nono', 1, 2) print '% s rf Ind t = % d' % (str, str. rfind ('T') print '% s count t = % d' % (str, str. count ('T') string replacement replace old with new: str. replace ('Old', 'new') replaces the old with new: str. replace ('Old', 'new', maxReplaceTimes) print '% s replace t to * = % s' % (str, str. replace ('T', '*') print '% s replace t to * = % s' % (str, str. replace ('T', '*', 1) string to remove spaces and to specify characters to remove spaces on both sides: str. strip () left space: str. remove right space from lstrip (): str. rstrip () removes the strings on both sides: str. strip ('D'), which also has lstrip, rstripstr =' Python String function 'print '% s strip = % s' % (str, str. strip () str = 'python String function' print '% s strip = % s' % (str, str. strip ('D') Splits string by specified character into an array: str. split ('') string. split (s, sep = None, maxsplit =-1) split s with sep and return the list after split. If sep is not provided or is None, the default space is str = 'a B c de 'print '% s strip = % s' % (str, str. split () str = 'a-B-c-de 'print '% s strip = % s' % (str, str. split ('-') string. the function of join is the opposite. L = string. split ("hello world") string. join (l) 'Hello world' join (list [, sep]) returns a string by combining the list with sep. String to determine whether the correlation starts with start: str. whether startswith ('start') ends with end: str. endswith ('end') is all letters or numbers: str. isalnum (): str. isalpha (): str. whether isdigit () is all lowercase: str. whether islower () is in uppercase: str. isupper () str = 'python String function' print '% s startwith t = % s' % (str, str. startswith ('T') print '% s endwith d = % s' % (str, str. endswith ('D') print '% s isalnum = % s' % (str, str. isalnum () str = 'pythonstringfunction' print '% s isalnum = % s' % (str, str. isalnum () print '% s isalpha = % s' % (str, str. isalpha () print '% s isupper = % s' % (str, str. isupper () print '% s islower = % s' % (str, str. islower () print '% s isdigit = % s' % (str, str. isdigit () str = '000000' print '% s isdigit = % s' % (str, str. isdigit () # replace string: replace (old, new [, max]) def replaceString (s): print s. replace ("hello", "hi") print s. replace ("hello", "world", 2) print s. the replace ("abc", "hi") applet traverses the functions of the python string and uses help to print the help information. [python] import string # print dir (string) funOrC = []; vars = []; for fv in dir (string): name = "string. % s "% fv if (callable (eval (name): funOrC. append (fv) else: vars. append (fv) print funOrC print vars for tmp in funOrC: # print tmp, "###", eval ("string. % s "% tmp) help (" string. % s "% tmp) print '************************************** *************'

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.