Python basics: Common operations for strings

Source: Internet
Author: User

# Slices

#Slice gets part of data in object [start position: End position (not included): Step]Qpstr ="Shandong Jacky"result= Qpstr[1:3: 1]#Dong ZhangPrint(Result)#Quick Pick up top tworesult = Qpstr[:2]Print(Result)#quickly take the latter tworesult = Qpstr[-2:]Print(Result)#get the entire string quicklyresult =qpstr[:]#quickly get string reversal stringresult = Qpstr[::-1]Print(Result)

# Find detects if STR is contained in MYSTR, or returns 1 if the index value is returned.

# mystr.find (str, start=0, End=len (mystr))

" ABC de "  = Mystr.find ("a")#  (specifies what to look for, start position, end position (not included))find = Mystr.find ("ABC", 0, 1)print(Find)

# Index is the same as the Find () method, except if STR does not report an exception in MyStr

# Mystr.index (str, start=0, End=len (mystr))

index = Mystr.index ("ABC", 0, Len (mystr))print(index)

# Count Returns the number of occurrences of str between start and end in MyStr

# Mystr.count (str, start=0, End=len (mystr))

Count = Mystr.count ("a", 0, Len (mystr))print(count)

# Replace replaces str1 in mystr with STR2 if Count is specified, no more than count times

# Mystr.replace (str1, str2, Mystr.count (STR1))

Replace = Mystr.replace ("C""a", Mystr.count ("A  "))print(replace)

# Split slices mystr with str, if Maxsplit has a specified value, only maxsplit substrings are delimited
# Mystr.split (str= "", 2)

Split = Mystr.split ("", 2)  #  result array format print(split)

Python basics: Common operations for strings

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.