Python String Method Learning notes

Source: Internet
Author: User

# One, string case conversion
# string First character uppercase
Print ("Hello World". Capitalize ())
# Change a string to a title
Print ("Hello World". Title ())
# Convert String to uppercase
Print ("Hello World". Upper ())
# Convert String to lowercase
Print ("HELLO World". Lower ())
# Flip the case in a string
Print ("Hello World". Swapcase ())

# Two, string segmentation
# with an element as a split point, divide the string into 3 parts, and the first element found from left to right is a split point
Print (' HelloWorld '. partition (' O '))
# divides a string into 3 parts with an element as a split point, and the first element found from right to left is a split point
Print (' HelloWorld '. rpartition (' O '))
# Replace the elements in the original string, replace all by default, you can specify a few (left to right number)
Print ("Hello World". Replace (' O ', ' a ', 1))
# Divide a string by dividing it from left to right by dividing it by a single element.
Print ("Hello World". Split (' O ', 1))
# Divide a string by dividing it from right to left n times with an element as a split point
Print ("Hello World". Rsplit (' O ', 1))
# Separated by line (' \ r ', ' \ r \ n ', \ nthe '), returns a list containing the rows as elements, if the argument keepends is False, does not contain a newline character, and if true, the newline character is preserved.
Print (' Hello\nworld '. Splitlines (True))
# Three, find elements in a string
# count the number of occurrences of a string from index n to y, by default to find in the entire string
Print ("Hello World". Count (' O ', 7, 10))
# finds an element between index [n, y] and defaults to [:] Returns the index of the element if no return-1 is found
Print ("Hello World". Find (' e '))
Print ("Hello World". Find (' O ', 0, 2))
# The index value of the element is found between [N, y] and no error is found
Print ("Hello World". Index (' o '))
Print ("Hello World". Index (' e ', 0, 5))
# Four, string judgment
# Determines whether a string starts with an element
Print (' HelloWorld '. StartsWith (' h '))
# Determines whether the string's index from [N,y] ends with a character and returns a Boolean value
Print ("Hello World". EndsWith (' E ', 0, 2))
# Determine if there are only numbers or letters
Print (' abc123 '. Isalnum ())
# to determine if it contains only letters
Print (' abc '. ISALPHA ())
# Determine if the letters are lowercase
Print ("Hello". Islower ())
# Determine if character is not a space
Print ("". Isspace ())
# determine if the string is not a caption (the first letter is not uppercase)
Print ("Hello World". Istitle ())
# inserts a specified character between elements
# Five, string formatting
# Center String, specify the total length of the string, not enough with other characters, the default is a space
Print ("Hello World". Center (20, "#"))
# replace \ t in string with n spaces
Print ("Hello\tworld". Expandtabs (TABSIZE=20))
Print (' # '. Join ("Hello World"))
# Specify the length of the output character, and left-aligned, the less part with the specified characters
Print ("Hello World". Ljust (20, "#"))
# Specify the length of the output character, and right-aligned, the less part with the specified characters
Print ("Hello World". Rjust (20, "#"))
# Remove the space to the left of the string
Print (' Hello '. Lstrip ())
# Remove the space to the right of the string
Print (' Hello '. Rstrip ())
# remove spaces on both sides of the string
Print (' Hello '. Strip ())
# Specifies the length of the string, not enough in front of the complement 0
Print ("123". Zfill (5))
# Concatenation of strings
Print (' Hello ' + ' world ')
Print (' Hello ' * 3)

Python String Method Learning notes

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.