Several string-handling functions in Python

Source: Internet
Author: User

1.eval ()

function: evaluates the string str as a valid expression and returns the result of the calculation.
Syntax: eval (source[, globals[, locals]), value
Parameters: 

    • Source: The code object returned by a Python expression or function compile ()
    • Globals: Not required. It must be dictionary.
    • Locals: not required. Arbitrary map Object
>**Example:** 1Can put the list,tuple, Dict and string convert each other.2============================ 3string conversion to List4 >>>A= [[ up], [3,4], [5,6], [7,8], [9,0]] " 5 >>>type(a)6 <type ' str '> 7 >>>B= Eval(a)8 >>> PrintB9[[1,2], [3,4], [5,6], [7,8], [9,0]]Ten >>> type(b) One <type ' list '> A ============================ -Convert strings into dictionaries - >>>A= "{1: ' A ', 2: ' B '}" the >>> type(a) - <type ' str '> - >>>B= Eval(a) - >>> PrintB +{1:' A ',2:' B '} - >>> type(b) + <type ' Dict '> A ============================ atString Conversion Narimoto Group - >>>A= ([up], [3,4], [5,6], [7,8], (9,0)) " - >>> type(a) - <type ' str '> - >>>B= Eval(a) - >>> PrintB in([1,2], [3,4], [5,6], [7,8], (9,0)) - >>> type(b) to <type ' tuple '>
2.split ()

function: slices A string by specifying a delimiter, separated as num substrings if the parameter num has a specified value. and returns the segmented list of strings.
Syntax:str.split (str= "", Num=string.count (str))
Parameters:

    • STR--the delimiter, which defaults to all null characters, including spaces, line breaks (\ n), tabs (\ t), and so on.
    • Num--the number of splits.

Example:
str = "Line1-abcdef \nLine2-abc \nLine4-abcd"; print str.split( ); print str.split(‘ ‘, 1 );
Output:
[‘Line1-abcdef‘, ‘Line2-abc‘, ‘Line4-abcd‘] [‘Line1-abcdef‘, ‘\nLine2-abc \nLine4-abcd‘]

3.join ()

function: An array of connection strings. Generates a new string from a string, tuple, or element in the list, with the specified character (delimiter) connection
Syntax: ' Sep '. Join (SEQ)
Parameters:

    • Sep: Delimiter. Can be empty
    • SEQ: sequence of elements to concatenate, strings, tuples, dictionaries
    • The syntax above is to combine all the SEQ elements into a new string with Sep as a delimiter
    • Return value: Returns a string that is generated after each element is concatenated with the delimiter Sep

      Example:

Several string-handling functions in Python

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.