Python Learning (iii)--string manipulation

Source: Internet
Author: User
Tags naming convention

# Manipulation of strings
# Features: Non-modifiable

name = ' Phoenix '
Print (Name.capitalize ()) # Capitalize first letter
# Show: Phoenix
Print (Name.casefold ()) # Uppercase Full Change lowercase
# Show: Phoenix
Print (Name.center (50, "*")) # output is centered in "Phoenix" and the other 50 characters are padded with "*"
# Display: *********************phoenix**********************
Print (Name.count (' Oen ') # Count of occurrences of "Oen"
# Display: 1
Print (Name.encode ()) # Encodes a string into bytes format
# show: B ' Phoenix '
Print (Name.endswith ("IX")) # to determine if the string ends with IX
# Display: Ture
Print ("Phoenix\ty". Expandtabs (12)) # convert \ t to how long spaces
# Display: Phoenix Y
Print (Name.find ("O")) # Find A, find return index, cannot find return-1
# Display: 2

# Format: Formatting

msg = "My name is {}, and ' age ' is {}." # by Location
Print (Msg.format ("Alex", 22))
# Display: My name is Alex, and the is 22.
msg = "My name is {1}, and ' age ' is {0}." # by serial number
Print (Msg.format ("Alex", 22))
# Display: My name is a, and age is Alex.
msg = "My name is {name}, and age is {age}." # by variable name
Print (Msg.format (age=22, name= "Alex"))
# Display: My name is Alex, and the is 22.

# Format_map:

Print ({' Age ': Msg.format_map, ' name ': ' Alex '}) # by Dictionary
# Display: My name is Alex, and the is 22.

Print (Msg.index ("a")) # returns the index of the string where A is located
# Display: 4
Print (' 9aA '. Isalnum ()) # to determine if a string is a number and a letter
# Display: True
Print (' 9 '. IsDigit ()) # to determine if a string is an integer
# Display: True
Print (Name.isnumeric ()) # detects if a string consists only of numbers
# Display: False
Print (Name.isprintable ()) # determines if the string is printable
# Display: True
Print (Name.isspace ()) # detects if a string consists only of spaces
# Display: False
Print (Name.istitle ()) # Determines if all the words in the string are spelled in uppercase, and the other letters are lowercase
# Display: False
Print (Name.isupper ()) # to determine if all the letters in the string are uppercase
# Display: False

Print ("|". Join ([' Alex ', ' Jack ', ' Rain ']) # concatenate the elements in the list into a new string with the specified characters
# Display: Alex|jack|rain

# Maketrans: The conversion table used to create a character map, the simplest way to invoke two parameters.
# The first argument is a string that represents the character that needs to be converted, and the second parameter is also the target of the string representation transformation.
# Note: The length of the two string must be the same for the one by one corresponding relationship.

Intab = "Aeiou"
Outtab = "12345"
# character:
Trantab = Str.maketrans (Intab, Outtab)
str = "This is string example .... WOW!!!"
Print (Str.translate (trantab))
# display: th3s 3s str3ng 2x1mp12 .... W4W!!!

Print (Msg.partition (' is ')) # used to split the string according to the specified delimiter
# Display: (' My Name ', ' is ', ' {name}, ' and '-age '}. ')

Print ("Alex Li, Chinese name is Lijie". Replace ("Li", "Li", 1)) # replaces the old (older string) in the string with the new character,
# If you specify a third parameter, Max, the replacement does not exceed Max times.
# Show: Alex LI, Chinese name is Lijie

Print (Msg.swapcase ()) # swaps the case of a string
# Display: MY NAME is {name}, and age is {age}.

Print (Msg.zfill (40)) # Returns a string of the specified length, the original string is right-aligned, the front padding 0
# Display: 00000my name is {name}, and age is {age}.

Print (Msg.ljust (40, "-")) # returns an original string left aligned and fills a new string of the specified length with a space. Returns the original string if the specified length is less than the length of the original string.
# Display: My name is {name}, and age is {age}.----

Print (Msg.rjust (40, "-")) # returns an original string right-aligned and fills a new string with a space of a specified length. Returns the original string if the specified length is less than the length of the original string.
# display:----My name is {name}, and ' age ' is {age}.

b= "ddefdsdff_ haha"
Print (B.isidentifier ()) # detects if a string can be used as a marker, that is, if a variable naming convention is met
# Display: True

Python Learning (iii)--string manipulation

Related Article

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.