Python character Basics

Source: Internet
Author: User

The Python programming language is a powerful object-oriented computer programming language. Its Powerful advantages can help us implement many functional requirements in actual programming. Today, we will first introduce the basic content about Python characters.

1. Representation of Python characters

A string consisting of single or double quotation marks.

 
 
  1. “abc” \  
  2. ‘def’ 

Represents a string, while "abc" + "def" are two strings connected together, the two are different. "Can be any long string in the middle

Ii. Python character operations

1. case-insensitive Conversion

 
 
  1. S. capitalize () # string s: uppercase letters
  2. S. lower () # lowercase for all
  3. S. upper () # convert all to uppercase
  4. S. swapcase () # case-insensitive swap
  5. Len (s) # obtain the string size

2. Search for substrings

 
 
  1. S. find (substring, [start [, end]), return index value, cannot find, return-1
  2. S. rfind (substring, [start [, end]) Reverse Lookup
  3. S. index (substring, [start [, end]) is similar to find (). If substring cannot be found,
  4. ValueError exception
  5. S. rindex (substring, [start [, end]) Reverse Lookup
  6. S. count (substring, [start [, end]) returns the number of times the substring is found.

3. format the string

Usage: s % <tuple> tuple indicates a parameter list. Each value in the tuple is represented by a string, and the format represented is determined by s.

 
 
  1. S. ljust (width) is left aligned. If width is greater than len (s), spaces are added. Otherwise, return s.
  2. S. Align ust (width) right alignment
  3. S. center (width) center
  4. S. lstrip () removes the white space on the left
  5. S. rstrip () removes the white space on the right
  6. S. lstrip () removes spaces on both sides

4. Merge and decompose Python characters

Merge: s. join (words)

Words is a tuple or list containing strings. Join concatenates strings in words into a string using the separator "s.

Example:

 
 
  1. >>> “+”.join([”hello”,”my”,”friedn”])  
  2. ‘hello+my+friedn’ 

Decomposition:

 
 
  1. s.split(words) 

Words is a string that represents a separator. The split operation is opposite to the join operation. Splits s into a list.

Example:

 
 
  1. >>> “hello my fried”.split(” “)  
  2. [’hello’, ‘my’, ‘fried’]  

The preceding section describes the Python characters.

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.