Python Learning note string (str)

Source: Internet
Author: User

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)

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.