Python Learning Note 2-string manipulation

Source: Internet
Author: User

One, string slices

# slices can also fit into the string tittle=' today's  hair apple ' for in Enumerate (tittle):  # enumerate can loop subscript and value    at the same time Print ('%s:%s' % (i,t))

Second, non-empty is true

# non-empty is true Print (1>2)

Three, String common operation

#mutable variables: list dic#immutable variable tuple str#The remove can not be removed when the list is recycled, which causes the subscript to change#Example:#can not use l2=l such memory address is the same, l change, L2 will also changel=[1,1,2,3,4,5,6,7,8]l2=l[:] forIinchL2:ifi%2!=0:l.remove (i)Print(l) name='besttest 123\n'new_name=name.strip ()#by default, both spaces and line breaks are removedNew_name2=name.lstrip ()#by default, the left space and line breaks are removedNew_name3=name.rstrip ()#RightNew_name4=name.count ('T')#find the number of occurrences of a character in a stringNew_name5=name.center (50,'a')#50 a,name in the middle of the placeIndex=name.find ('s')#find the string subscript, can not find return-1, the same return to the first subscriptIndex2=name.index ('s')#find the string subscript, can not find the errorName.upper ()#Turn all letters into uppercaseName.lower ()#Turn all letters into lowercasefile_name='A.xls'Print(File_name.endswith ('. xls'))#determines whether a string ends in XLSF='{Name} welcome'Print(F.format (name='Suki'))#string Formattingd={'name':'Suki'," Age":' -'}Print(D.format_map (d))#string formatting, passed in is a dictionarySql='SELECT * from user'New_sql=sql.replace ('Select','Update')#string Substitution#The most important methodst='a,b,c,d,e,f,g'St.split (',')#remove comma, string into list#List Variable stringsl=['a','b','C','D','e']','. Join (SL)

Python Learning Note 2-string manipulation

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.