Introduction to common python modules string Module

Source: Internet
Author: User
Tags string methods

Print? #/Usr/bin/python

#/Usr/bin/python


1. Overview

 


The string module provides functions for processing string types.
 

File: string-example-1.py import string text = "Monty Python's Flying Circus" print "upper", "=>", string. upper (text) # print "lower", "=>", string. lower (text) print "split", "=>", string. split (text) # string segmentation print "join", "=>", string. join (string. split (text), "+") # string connection print "replace", "=>", string. replace (text, "Python", "Java") # replace print "find", "=>", string. find (text, "Python"), string. find (text, "Java") # string SEARCH print "count", "=>", string. count (text, "n") # string count upper => monty python's flying circus lower => monty python's flying circus split => ['monty ', "Python's", 'flying ', 'cirocs'] join => Monty + Python's + Flying + Circus replace => Monty Java's Flying Circus find => 6-1 count => 3 import stringtext = "Monty python's Flying Circus "print" upper ", "=>", string. upper (text) # print "lower", "=>", string. lower (text) print "split", "=>", string. split (text) # string segmentation print "join", "=>", string. join (string. split (text), "+") # string connection print "replace", "=>", string. replace (text, "Python", "Java") # replace print "find", "=>", string. find (text, "Python"), string. find (text, "Java") # string SEARCH print "count", "=>", string. count (text, "n") # string count upper => monty python's FLYING CIRCUSlower => monty python's flying circussplit => ['monty ', "Python's", 'flying ', 'cirocs'] join => Monty + Python's + Flying + Circusreplace => Monty Java's Flying Circusfind => 6-1 count => 3

In Python 1.5.2 and earlier versions, string uses functions in strop to implement module functions. in Python1.6 and later versions, more string operations can be accessed as string methods. Many functions in the string module only encapsulate the corresponding string methods.

 


2. Use the string method to replace the string module function

 

File: string-example-2.py text = "Monty Python's Flying Circus" “upper", "=>", text.upper() "lower", "=>", text.lower() "split", "=>", text.split() "join", "=>", "+".join(text.split()) "replace", "=>", text.replace("Python", "Perl") "find", "=>", text.find("Python"), text.find("Perl") "count", "=>", text.count("n") upper => MONTY PYTHON'S FLYING CIRCUS lower => monty python's flying circus split => ['Monty', "Python's", 'Flying', 'Circus'] join => Monty+Python's+Flying+Circus replace => Monty Perl's Flying Circus find => 6 -1 count => 3 text = "Monty Python's Flying Circus"“upper", "=>", text.upper()"lower", "=>", text.lower()"split", "=>", text.split()"join", "=>", "+".join(text.split())"replace", "=>", text.replace("Python", "Perl")"find", "=>", text.find("Python"), text.find("Perl")"count", "=>", text.count("n")upper => MONTY PYTHON'S FLYING CIRCUSlower => monty python's flying circussplit => ['Monty', "Python's", 'Flying', 'Circus']join => Monty+Python's+Flying+Circusreplace => Monty Perl's Flying Circusfind => 6 -1count => 3

To enhance the character processing capability of the module, in addition to the string method, the string module also contains the type conversion function used to convert the string to other types (as shown in Example 1-53 ).

 


3. Use the string module to convert a string to a number.

 

File: string-example-3.py import string int ("4711"), string. atoi ("4711"), string. atoi ("11147", 8), # octal string. atoi ("1267", 16), # hexadecimal string. atoi ("3mv", 36) # whatever... print string. atoi ("4711", 0), print string. atoi ("04711", 0), print string. atoi ("0x4711", 0) print float ("4711"), print string. atof ("1"), print string. atof ("1.23e5") 4711 4711 4711 4711 4711 4711 2505 18193 4711.0 1.0 123000.0 import stringint ("4711"), string. atoi ("4711"), string. atoi ("11147", 8), # octal string. atoi ("1267", 16), # hexadecimal string. atoi ("3mv", 36) # whatever... print string. atoi ("4711", 0), print string. atoi ("04711", 0), print string. atoi ("0x4711", 0) print float ("4711"), print string. atof ("1"), print string. atof ("1.23e5") 4711 4711 4711 4711 47114711 2505 181934711.0 1.0 123000.0

In most cases (especially when you are using version 1.6 or later), you can use the int and float functions to replace the corresponding functions in the string module. The atoi function can accept the optional second parameter, specifying the number base ). if the number base is 0, the function checks the first few characters of the string to determine the number base used: If it is "0x," the number base is 16 (hexadecimal ), if it is "0,", the number base is 8 (octal ). the default base value is 10 (decimal). This value is used when no parameter is passed. in Versions 1.6 and later, the int function and atoi can accept the second parameter. unlike the string version functions, int and float can accept Unicode string objects.

 


4. Analysis of Some Functions

 

4.1 function prototype strip
Declaration: "s" is a string and "rm" is the sequence of characters to be deleted.

S. strip (rm) Delete the characters starting and ending in the s string and located in the rm Delete Sequence

S. lstrip (rm) Delete the characters starting from the s string in the rm Delete Sequence

S. rstrip (rm) deletes the characters at the end of the s string in the rm deletion sequence.

Note:

4.1.1. When rm is blank, blank spaces (including '\ n',' \ R', '\ t', '') are deleted by default ','')

For example:

>>> A = '000000'
>>> A. strip ()
'123'
>>> A = '\ t \ tabc'
'Abc'
>>> A = 'sdff \ r \ N'
>>> A. strip ()
'Sdff'
4.1.2. Here, the rm Delete sequence is deleted as long as the characters on the edge (beginning or end) are in the delete sequence.

For example:

>>> A = '123abc'

>>> A. strip ('21 ')
'3abc' results are the same
>>> A. strip ('12 ')
'3abc'


4.2split Method


The split method of python strings is often used. For example, we need to store a long data and store it in a structured way to facilitate data retrieval and processing in the future. Of course, it can be in json format. However, data can also be stored in a field, and some identifier can be used to separate it, for example, the format of our storage:

Format:

Name, age | Name of another user, age

Name: haha, age: 20 | name: python, age: 30 | name: fef, age: 55

Then we can use the split method of the string object to cut the string object into a list.

A = 'name: haha, age: 20 | name: python, age: 30 | name: fef, age: 55'

Print a. split ('| ')

Returned results:

['Name: haha, age: 20', 'name: python, age: 30', 'name: fef, age: 55']

# List objects are automatically separated by commas (,).

 


4.3join Method


List = [1, 2, 3, 4, 5, 6, 7]
','. Join (str (I) for I in list) # str (I) for I in list can be successfully written for this reason,

 

Join is a function of the string type. It calls its string to connect to the list in the parameter ','. the join caller is ',', and all objects in python are objects. ',' is a string type object, and the join function is called, concatenates the values in the subsequent list with commas to a new string. str (I) for I in list is a ing, which converts each value in the list to a string. If you want str (I) for I in list, the result is ['1', '2', '3', '4', '5', '6 ', '7']
String. join (sep): use string as the delimiter to combine all elements (represented by strings) in sep into a new string.

>>> Li = ['my, 'name', 'is, 'bob']

>>> ''. Join (li)
'My name is bob'
 
>>> '_'. Join (li)
'My _ name_is_bob'
 
>>> S = ['my, 'name', 'is, 'bob']
>>> ''. Join (s)
'My name is bob'
 
>>> '..'. Join (s)
'My .. name... is .. bob'

 


 

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.