python--string

Source: Internet
Author: User

# string method does not change the value of the original string
Name = ' besttest '
Print (Name.strip ()) # Default minus spaces and line breaks on both sides
Print (name)

Print (' 123 456 '. Strip (' 456 '))

New_name = Name.lstrip () # Remove the left space and line break
new_name1 = Name.rstrip () # Remove the right space and line break
Print (Name.count (' t '))  # Find the number of occurrences of a string
Print (Name.count (' O '))
name = ' Besttest '
Print (Name.capitalize ()) # Capitalize first letter

Print (Name.center, ' * ') # put the string in the middle, with a total length of 50, to fill the whole
Print (Name.find (' t '))  # find T, return the first occurrence of subscript
Print (Name.find (' O ')) # If not present, return-1

Print (Name.index (' t '))
# Print (Name.index (' O ')) # no time to find an error!

Print (Name.upper ())  # lowercase letters to uppercase
Print (Name.lower ()) # Uppercase to lowercase

name = ' A.txt '
Print (Name.endswith (' txt ')) # returns TRUE or False if the string ends with
Print (Name.startswith (' a ')) # returns TRUE or False if the string starts with what

f = ' {name} welcome '
Print (F.format (name= ' Wangsilei ')) # string formatting

f = ' {name} welcome, {age} '
D = {' name ': ' Panda ', ' age ': 18}
Print (F.format_map (d))
name = ' ABCDE '
Print (Name.replace (' abc ', ' 123 ')) # string substitution

Print (' 123456 '. IsDigit ()) # is the number
Print (' abc '. isupper ()) # to determine if all uppercase letters are
Print (' abc '. islower ()) # to determine if all lowercase letters are

Print (' abc123 '. Isalnum ()) # contains numbers or letters (only numbers or alphabetic combinations)
Print (' AbA '. Isalpha ()) # is the English alphabet
# Important methods of string
str1 = ' A, b, C, D, E, F, G '
Print (Str1.split (', '))
Print ((Str1.split ('))) # string split, return a list
Print ((Str1.split (' B ')))
Print ((Str1.split ())) # do not write default space-delimited

Slit = [' A ', ' B ', ' C ', ' d ']
Name = ', '. Join (Slit) # through, link each value inside the list
Print (name)

S2 = ' hhhhhhhhh '
Print (' * '. Join (S2))

D = {' name ': ' Wangsilei ', ' Age ': 18}
Print (' * '. Join (d)) # only valid for key

name = ' ABCDE '
Print (' A ' in name)
Print (' e ' not in name)

python--string

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.