Python 0-basic entry-5 string formatting and built-in functions of sequence

Source: Internet
Author: User
Python 0-basic entry-5 string formatting and built-in functions of sequences first of all, let's briefly talk about tuples. tuples joke about sequences with shackles, this is because tuples do not perform a series of operations on their elements as they do in sequence. Once a tuples are defined, the elements in the tuples cannot be changed at will.
Definition of tuples: Names of tuples = (,,,,,,)
Note that the mark of the tuples is not (),,
Yuan zu said so much. Next, let's talk about the problem of string formatting.
Don't worry, just pick up the goods directly.

# Print string formatting ("------ use of the Format function ------") # The Format function contains unknown parameters and keyword parameters # replacement field replaced by {} # The preceding parameter is called Unknown, keyword parameter print ("{0} love {1} {2 }". format ("I", "FishC", "com") print ("\ n") # keyword parameter # This write is incorrect because the compiler does not know, who corresponds to # print ("{a} love {B} {c }". format ("I", "FishC", "com") print ("{a} love {B} {c }". format (a = "I", B = "FishC", c = "com") # or use the parameter together with an unknown parameter, however, # unknown parameters must be printed ("\ n") print ("{0} love {B} {c}" before the keyword parameter }". format ("I", B = "FishC", c = "com") # print curly braces using escape characters ("\ n ") print ("{0 }}". format ("escape print curly braces") # colon indicates the start print ("{0 :. 1f} {1 }". format (27.658, "GB "))

The output result is:

Let's talk about sequence-related issues. sequences are collectively referred to as lists, tuples, and strings. they are put together because there are some commonalities between them. What we learn today is the built-in function of sequence, which can be used directly.

# Sequence # list tuples are collectively referred to as sequences # The list () function is used to convert an iteratable object to a list # If no parameter exists, generate an empty list # if there is a parameter, this parameter is the iterator print ("use the list () function to generate an empty list") a = list (); print (a, "\ n ") # The process of the list function can be understood as first creating an empty sequence # then using the for loop to move elements to the new sequence using the index function print ("using list (iterator) the function generates a non-empty list ") a =" I Love You "atolist = list (a) print (atolist) print (" \ n ") # tuple ([iterator]) convert an iteratable object to a tuple atotuple = tuple (a) print (atotuple) print ("\ n") # str (obj) function: convert an obj object to a string, forced type conversion # len (sub) function: returns the length of the sequence # max (), min () function ensures that the data types in the sequence are unified # max () function: returns the maximum value print (max (, 5) in the sequence or parameter set # min () function: returns the maximum value in the sequence or parameter set # sum (iterable [, start = 0]) returns the sum of the sequence iterable and the optional parameter start # the data types in the sequence must also be consistent, and must be print ("\ n") tuple1 =, 3, 4, 5) print (sum (tuple1) print ("\ n") # ** sorted ** function to sort the sequence, the default value is from small to large # note that the list1 = [, 6] list1 = sorted (list1) print (list1) print ("\ n") is distinguished from the built-in function sort of the list ") # ** reversed () ** to reverse sequence elements, but not returned # note the built-in function reverse () with the list () differentiation # It is an iterative object print (reversed (list1) print ("\ n") # we can use the objects of the returned iterator as list (iterator) parameter # returns a list of print (list (reversed (list1) print ("\ n") # zip synthesis sequence a = [1, 2, 4, 4, 5, 6, 7, 8] B = ["a", "B", "c", "d"] zip (a, B) print (list (zip (a, B )))

The program runs as follows:

The preceding section describes the formatting of the five strings in the basic Python environment and the built-in functions of the sequence. For more information, see The PHP Chinese website (www.php1.cn )!

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.