Python tutorial 3 ---- Strings and Console Output

Source: Internet
Author: User
Tags value of pi

Section 1 Python also has a good data type: String 2. A String is a String written in ''or" ". 3. Set the brian variable to" Always look on the bright side of life.! "[Python] # Set the variable brian on line 3! Brian = "Always look on the bright side of life! "Section 2 1 Exercise 1 set the variable caesar to Graham 2 set the variable praline to john 3 set the variable viking to Teresa [python] # Assign your variables below, each on its own line! Caesar = "Graham" praline = "John" viking = "Teresa" # Put your variables above this line print caesar print praline print viking section 3 1 Python implements escape characters through \ in step 2, 'Help! Help! I'm being repressed! 'In I' m to escape [python] # The string below is broken. Fix it using the escape backslash! 'Help! Help! \ '\ M being repressed! 'Section 4 1 we can use "" to avoid the emergence of escape characters. Exercise 2: set The variable mongoth_letter to The fifth character of MONTY [python] "The string" PYTHON "has six characters, numbered 0 to 5, as shown below: + --- + | P | Y | T | H | O | N | + --- + + 0 1 2 3 4 5 So if you wanted "Y ", you coshould just type "PYTHON" [1] (always start counting from 0 !) "Effecth_letter =" MONTY "[4] print effecth_letter section 5 1 describes the first method of String. len () Evaluate the length of a String 2 exercise: set the value of the parrot variable to "Norweigian Blue", and then print the length of parrot [python] parrot = "Norwegian Blue" print len (parrot) section 6 1 describes the second method of String. lower () converts all uppercase letters into lowercase letters. Exercise 2: converts uppercase letters in parrot to lowercase letters and prints [python] parrot = "Norwegian Blue" print parrot. lower () Section 7 1 describes the third method of String. upper () converts all uppercase letters to lowercase letters 2. exercise: converts lowercase letters in parrot to uppercase letters and prints [python] pa Rrot = "norwegian blue" print parrot. section 8 1 of upper () describes the fourth method of String. str () converts a non-String to a String. For example, str (2) converts 2 to a String "2" 2 ". exercise: set the pi value of a variable to 3.14 and convert pi to the string [python] "" Declare and assign your variable on line 4, then call your method on line 5! "Pi = 3.14 print str (pi) Section 9 1 describes". for example, the four methods of the above four strings all use the point 2 exercise: use ". use The String variables ministry functions len () and upper (), and print out [python] ministry = "The Ministry of Silly Walks" print len (ministry) print ministry. section 10 of upper () 1 describes the role of print 2 exercises: print the output string "Monty Python" [python] "" Tell Python to print "Monty Python" to the console on line 4! "Print" Monty Python "section 11th 1 describes print to print a variable. 2. Exercise: Assign the value of the_machine_goes to the variable." Ping! ", Then print out [python]" "Assign the string" Ping! "To the variable the_machine_goes on line 5, then print it out on line 6! "The_machine_goes =" Ping! "Print the_machine_goes Section 1 describes how to use + to connect two String 2 exercises: use + to connect the three strings "Spam" and "and" eggs "to output [python] # Print the concatenation of" Spam and eggs "on line 3! Print "Spam" + "and" + "eggs" section 13th 1 describes the function of str () to convert a number into a string. 2. Exercise: Use str () the function converts 3.14 to a string and outputs [python] # Turn 3.14 into a string on line 3! Print "The value of pi is und" + str (3.14) Section 1 describes how to format a string by %. The string is % s 2. For example, there are two strings, format % s to output [python] string_1 = "Camelot" string_2 = "place" print "Let's not go to % s. 'Tis a silly % s. "% (string_1, string_2) Section 15th 1 review previous content 2 Exercise 1 set the value of the variable my_string 2 print the length of the variable 3 use upper () function and print the variable value [python] # Write your code below, starting on line 3! My_string = "chenguolin" print len (my_string) print my_string.upper ()

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.