Stupid way to learn Python (vi)

Source: Internet
Author: User

Exercise 6: Strings (string) and text

Although you have already written strings in the program, you have not learned their usefulness. In this chapter we will use complex strings to create a series of variables from which you will learn what they are used for. Let's start by explaining what a string is.
A string usually refers to a small piece of character that you want to show to someone else, or that you want to "export" from the program. Python can recognize a string by using double quotes "or single quotes" in the text. You've seen it many times in your previous print exercises. If you put single quotes or double-quoted text behind print, they will be printed by Python.
The string can contain the formatted character%s, which you have seen before. You just put the formatted variable in the string, followed by a percent percent (percent), followed by the variable name. The only thing to be aware of is that if you want to put multiple variables into a string by formatting characters, you need to place the variables in () parentheses (parenthesis), and the variables are separated by commas (comma). Just like you go to the store and say, "I want to buy milk, bread, eggs, eight treasures porridge," just as programmers say "(milk, eggs, bread, soup)."
We will type a large number of strings, variables, and formatting characters, and print them out. We'll also practice using the shorthand variable name. Programmers like to use shorthand for annoying difficulty to save typing time, so let's learn this early so that you can read and write these things.

1x ="there is%d types of people."% 102binary ="binary" 3Do_not ="don ' t" 4y ="those who know%s and those who%s."%(binary, Do_not)5  6 Printx7 Printy8 9 Print "I said:%r."%xTen Print "I also said: '%s '."%y One  AHilarious =False -Joke_evaluation ="ISN ' t that joke so funny?!%r"  -  the PrintJoke_evaluation%Hilarious -  -W ="This was the left side of ..."  -E ="a string with a right side."  +  - PrintW + E
View Code

Bonus points Exercise

1. Read through the program and write a line of comments on each line to explain to yourself what this line does.

2. Find all the "string contains string" locations, with a total of four locations.

3. Are you sure there are only four locations? How do you know? Maybe I'm lying to you.

4. Explain why W and E can generate a longer string by using the + link.

Exercise Exercises

Dumb Way to learn Python (vi)

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.