Python's common operation with strings

Source: Internet
Author: User
Tags string format

I. Slicing a string by index

A slice operation (slice) can get a substring (part of a string) from a string. We define a shard using a square bracket, a start offset start, a terminating offset end, and an optional step step.

Format: [Start:end:step]

    • Start default value, if step is positive, the default is 0, if step is negative, then the default is-1
    • End default value, if step is positive, the default is the end of the string position, if the step is positive, the default is the beginning of the string position
    • Step default is 1. A positive number is sliced to the right, and if negative, slices to the left

Cases:

S = ' Abcdefghi '
print (s[::])
print (S[: 3:])
print (S[:3:-< span style= "color: #6897bb" >1])
print (S[ print (S[2::- 1])

Operation Result:

Abcdefghi
Abc
Ihgfe
Cdefghi
Cba

  

Two. Common methods for string manipulation

All uppercase Str.upper () letters all lowercase str.lower () Case Flip str.swapcase () capitalize str.capitalize () Note: All letters are lowercase, and if the first character is a letter, the letter is converted to uppercase. Long characters are separated by a symbol (number, space), each separated by a string with the first letter capitalized Str.title () Note: All letters are lowercase, if the first character is a letter to the uppercase string to the center (return a original string centered, and use a space to fill the width of the new string. The default padding character is a space) Str.center (width[, Fillchar]) gets the string length L=len (s) determines whether the string starts with the specified substring str.startswith (str, Beg=0,end=len (String) Note: The range is [Beg,end] The index of the lookup element in the string Str.find (sub,_start,_end) Str.index (sub,_start,_end) Note: Find can not find return -1,index cannot find an error, The element is a substring, which returns the first letter index of the substring to delete the specified string beginning at the end of the specified character Str.strip (chars) str.rstrips (chars) str.lstrips (chars) Note: The default delete space Computes the number of occurrences of a character in a string Str.count (x,_start,_end) Split string Str.split (Sep,maxsplit)

Three. String format output

1. How to pass the% placeholder

Cases:

"My name is%s,
My age is%d,
My height is%d "% (' Zhang San ',176)
Print (MSG1)
"My name is% (name) s,
My age% (ages) d,
My height% (height) d ' '%{' name ':' Zhang San ', ' age': +,' height ':176}
Print (MSG2)

2. Use the Format method

Cases:

MSG3 ="' MSG3
My name is {},
My Age {},
My height {} ". Format (' Zhang San ',18,176)
Print (MSG3)
MSG4 ="' Msg4
My name is {0},
My age is {1},
my height {2} ". Format ( ' Zhang San ' 18,176 "
print (MSG4)
Msg5 = " "Msg5
my name is { Name},
my Age {ages},
my height {height} ". Format (name= ' Zhang San ' , age=18,height=176"
print (MSG5)

Span style= "font-size:15px" > think: Can I write a formatted output method

Python's common actions on 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.