Python string manipulation

Source: Internet
Author: User
Tags uppercase letter

1. Capitalize the first letter of the string

>>> name = "Xiaoming"

>>> print (Name.capitalize ())//capitalize method to capitalize the first letter of a string

Xiaoming


2. Count the number of characters in the string

>>> name = "Xiaoming"

>>> Print (Name.count ("I"))

2


3. Repeat to print a character

>>> Print (Name.center (20, "-"))//A total of 20 characters, put "xiaoming" in the middle

------xiaoming------


4. Judge what the string ends with

>>> name = "Xiaoming"

>>> Print (Name.endswith ("ng"))

True


5. Find index position according to character

>>> name = "My name is Xiaoming"

>>> Print (Name.find ("is"))//find only the position of the beginning letter of the found character

8

>>> Print (Name[name.find ("is"):])//string also supports slicing

Is Xiaoming


6. Formatted output of string

>>> name = "My name is {name} and I am {year} years old."

>>> Print (Name.format (name= "Xiaoming", year=20))//format formatted output

My name is xiaoming and I am-years old.


>>> print ({"Name": "Xiaoming", "Year": Name.format_map})//format_map can be transferred in a dictionary format

My name is xiaoming and I am years old.


7, judge the string is not a pure English character composition

>>> name = "ABC"

>>> print (Name.isalpha ())

True

>>> name = "AB12"

>>> print (Name.isalpha ())

False


8. Determine if the number is an integer

>>> print ("AA". IsDigit ())

False

>>> print ("one"). IsDigit ())

True

>>> print ("11.2". IsDigit ())

False


9. Determine if the string is lowercase

>>> name = "ABC"

>>> print (Name.islower ())

True

>>> name = "My"

>>> print (Name.islower ())

False


10. Determine if a character in a string begins with an uppercase letter

>>> name = "My Name"

>>> print (Name.istitle ())

False

>>> name = "My name"

>>> print (Name.istitle ())

True


11. Determine if the string is all uppercase letters

>>> name = "MY name"

>>> print (Name.isupper ())

True


12. Simple use of Join

>>> print ("". Join (["1", "2", "3"])

123

>>> print ("+". Join (["1", "2", "3"])

1+2+3


13. Use of Ljust () and Rjust ()

>>> Name

' MY NAME '

>>> Print (Name.ljust (20, "*"))//Note: is the entire string length, "MY name" is a total of 7, so "*" only 13

MY name*************

>>> Print (Name.rjust (20, "*"))

MY NAME


14. Case Conversion of strings

>>> Name

' MY NAME '

>>> print (Name.lower ())//lower is to turn uppercase into lowercase

My Name

>>> print (Name.upper ())//upper is to convert lowercase to uppercase

MY NAME


15. Remove the carriage return or space at both ends of the string

>>> print ("\nmy name")


My Name

>>> print ("\nmy name". Lstrip ())//lstrip to remove spaces or carriage returns to the left of the string

My Name

>>> print ("My name\n")

My Name


>>> print ("My name\n". Rstrip ())//rstrip to remove the space to the right of the string or enter

My Name


>>> print ("\nmy name\n". Strip ())//strip to remove spaces or carriage returns from both ends

My Name


16. Substitution of characters

>>> print ("ABCA". Replace ("A", "L"))//default = Replace All

Lbcl

>>> print ("ABCA". Replace ("A", "L", 1))//You can specify

Lbca


17. String segmentation into a list

>>> print ("My name is Xiaoming". Split ())//default is a space

[' My ', ' name ', ' is ', ' xiaoming ']

>>> print ("My name is Xiaoming". Split ("I"))//You can specify a delimiter

[' My name ', ' s X ', ' aom ', ' ng ']


18, Swapcase, the lowercase in the string to uppercase, uppercase to lowercase

>>> print ("My name is". Swapcase ())

MY NAME is


Python 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.