How to perform Python string operations?

Source: Internet
Author: User

Python is simple, free, open-source, high-level language, portability, explanatory, object-oriented, scalable, embedded, and rich library, python is indeed a wonderful and powerful language. It reasonably combines high performance and makes programming simple and interesting. The following describes the python string operation process.

To use the string method, you must first import the string method. However, due to the suggestions of many python users, the string method is changed from python2.0 to S. method (), as long as S is a string object, it can be used in this way, without the need for import. At the same time, in order to maintain backward compatibility, the current Python still retains a string module.

  • Notes on compiling Python programs
  • What is the Python interpreter and its usage?
  • In-depth analysis of Python Web applications
  • How to install Python correctly?
  • Analysis of Python global variables in Python

The defined methods are the same as the python string operations. All these methods finally point to the functions called using S. method. Note that S. method () can call more methods than the string module. For example, isdigit () and istitle () can only be called in the S. method () method. For a String object, the first thought of operation may be to calculate the number of characters it contains, and it is easy to think of S. len (), but this is wrong. It should be len (S ). Because len () is a built-in function, including in the _ builtin _ module.

Python string operations do not include len () in the string type. At first glance, it seems a bit incomprehensible. In fact, everything has its own logic. Len () can calculate not only the number of characters in a string. It can also calculate the number of list members and the number of tuple members. Therefore, it is inappropriate to calculate len () in a single string. Therefore, len () can be used as a general function.

Using overload to perform different types of operations, you can also include a len () function in each type of len () operation. Python chooses the first solution. Similarly, the str (arg) function represents arg in the string type.
 
Case-insensitive conversion of characters in a string:

 
 
  1. S. lower () # lower case
  2. S. upper () # uppercase
  3. S. swapcase () # case-insensitive swap
  4. S. capitalize () # uppercase letters
  5. String. capwords (S)

Alignment of string in output:

 
 
  1. S. lower () # lower case
  2. S. upper () # uppercase
  3. S. swapcase () # case-insensitive swap
  4. S. capitalize () # uppercase letters
  5. String. capwords (S)

String search and replacement:

 
 
  1. S. lower () # lower case
  2. S. upper () # uppercase
  3. S. swapcase () # case-insensitive swap
  4. S. capitalize () # uppercase letters
  5. String. capwords (S)

Use the post-production translation table of the above function to translate S and delete some characters in deletechars. Note that if S is a unicode string, the deletechars parameter is not supported. You can use the same function to translate a character into a python string operation. In addition, you can use the functions of the codecs module to create more powerful translation tables. The string also has a pair of encoding and decoding functions:

 
 
  1. # Whether it is all letters and numbers with at least one character
  2. S. isalpha () # whether it is all letters with at least one character
  3. S. isdigit () # whether it is all numbers with at least one character
  4. S. isspace () # whether it is all blank characters with at least one character
  5. S. islower () # Are all lowercase letters in S?
  6. S. isupper () # Is the uppercase letter in S?
  7. S. istitle () # whether S is capitalized
     

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.