<< Chapter < Page Chapter >> Page >

An expression using literals

On the other hand, the following expression describes the sum of two literal numeric values:

sum = 6 + 8

No matter when this expression is evaluated, it will always produce a sum of 14.

String literals

Literal values can also be used for strings. For example, the interactive code fragment in Figure 1 shows

  • My name entered three times, in three different ways, on the interactive command line.
  • The output from the interpreter for each entry.
Figure 1 . String literals.
>>>"Dick Baldwin" 'Dick Baldwin'>>>'Dick Baldwin' 'Dick Baldwin'>>>Dick Baldwin SyntaxError: invalid syntax>>>

The first two entries are valid string literals. As you can see, in the first two cases, the interpreter displays my name in the output.

Note that in the first two cases, my name is surrounded by either quotes (sometimes called double quotes) or apostrophes (sometimes called single quotes) .

A syntax error

However, the third entry is not a valid string literal, and the interactive interpreter produced a SyntaxError message. (Although not shown in Figure 1 , the IDLE GUI interface highlighted my last name with a red backgroundimmediately above the SyntaxError message.) In the third case, my name is not surrounded by either double quotes or single quotes, and that is what causedthe error.

A valid string literal

Referring to String and Bytes literals , the Python Language Reference states:

Both types of literals can be enclosed in matching single quotes (') or double quotes (").

This explains why the first two input lines in the interactive code fragment in Figure 1 were accepted and the third input line produced an error.

Proper syntax

In the first input line in Figure 1 , my name was surrounded by matching double quotes. In the second input line, my name was surrounded by matching single quotes.

Bad syntax

However, in the third input line, my name was not surrounded by quotes of either type and this produced a syntax error.

More examples

Figure 2 shows two more examples of valid string literals.

Figure 2 . Triple-quoted strings.
>>>"""Dick Baldwin""" 'Dick Baldwin'>>>"""Dick Baldwin"""'Dick\nBaldwin'>>>===================================>>>"""Dick Baldwin""" 'Dick Baldwin'>>>"""Dick ... Baldwin"""'Dick \nBaldwin'>>>

The top half of Figure 2 was produced by the IDLE GUI interface. The bottom half was independently produced by the Python command-line interface.I included both of them to show you the output format difference between the two interfaces. In both cases, I pressed the Enter key following the input that reads """Dick.

Triple-quoted strings

Referring again to String and Bytes literals, the earlier quotation from the Python Language Reference goes on to state:

They can also be enclosed in matching groups of three single or double quotes (these are generally referred to as triple-quoted strings ). The backslash (\) character is used to escape characters that otherwise have aspecial meaning, such as newline, backslash itself, or the quote character.

Further down that same page we find:

Questions & Answers

price paid by consumers after the sales tax is called?
Pinias Reply
why government impose price floor on certain products?
how can black market be occurred when price ceiling is introduced?
Is it necessary to make decision when it fails you
Evelin Reply
Pls when what fails u
I think so
well i might naught know what you on about but i gotta tell you, it is necessary
how can the demand side approach solve unemployment
Tshepiso Reply
demand side approach to solve unemployment
Tshepiso Reply
no get ur questions
differentiate between choice and scale of preference
choice are the various wants of every individual whiles scale of preference is the list of unsatisfied wants arranged in order of important priority
choice refers to the act of selecting from alternatives or it refers to the act of choosing one thing instead of the other but Scale of preference refers to the listing of wants in order of important by providing the pressing need on top and the less pressing need at the bottom
four cardinal types of demand
Henry Reply
what are the types of demand
they are the derived,complementary,substitute and supplementary demands
What is equilibrium
equilibrium is the state of balance where there is no tendency for a change to occur.
Derived, competitive, complementary and composite demands
why is economics said to be a dismal science?
hi friends
fyn what is meaning of monopoly
monopoly is when a particular good is produced without competing with any other good.
good evening
draw the market demand curve from the individual demand curve?
With the aid of diagrams,compare the short run equilibrium positions of a perfect competitor and an imperfect competitors
Monlay Reply
What is the term consultation in economics?
Nthabiseng Reply
why is it that the long run Average cost curve does not touch that of the short run curve at its minimum?
Baah Reply
In few words, what is the role of interest rate in economy?
Carlos Reply
what is population
Nyakeh Reply
total number of people in a given area or country
the total number of people at a given area or country
total number of people in a given area or country
What is price Elasticity of demand?.
The responsiveness of the quantity of a commodity demanded to a change in its price, expressed as the percentage change in quantity demanded divided by the percentage change in price.
why is it that the long run Average cost doesn't touch the short run cost curve at its minimum?
what is supply
please rice and beans will be what type of demand but note we mostly cook it together
Oladosu Reply
complementary or joint demand
but did you know you can demand for rice without beans so how is it joint or complementary
From my point of view, rice and beans are replaceable goods, hence they can't be complementary.
what are the money value
Wisdom Reply
Nothing more than a purchase power, in other words, $100 now, must have the same value after 1 year.
what is Monopoly
Rebecca Reply
what is money
Lawal Reply
It can be define as a big transaction that can control any business for one place to another base.
money is recognisable note to accept both parties selling and buying
i don still understan
money is anything generally accepted as a medium of exchange
Money is anything generally accepted as a medium of exchange and for the settlement of goods and services .
hi good ppl, pls help out
discuss human and natural resources as develop strategies ro improving living condition of citizens in developing countries.
I don't understand the question.
it's a form of currency used for 2 or more individuals or parties in order to reach their amicable personal or business attainment. one must understand that money itself can manifest in multiple fashions for which the individuals or parties adheres.
are u trying to say we shld discuss ways in which human natural resources help in improving living condition of citizens in developing countries?
money is a legal thunder generally accepted as a medium of exchange for the payment of debt ,goods and services
money is a way of payment.
money is any thing that is generally accepted as a medium of exchange good for good and settlement of debt and means of payment
money is nothing but a object which is used for exchange of goods and services.
money is anything that is generally accepted as payment of goods and services and settlement of debt
Researchers demonstrated that the hippocampus functions in memory processing by creating lesions in the hippocampi of rats, which resulted in ________.
Mapo Reply
The formulation of new memories is sometimes called ________, and the process of bringing up old memories is called ________.
Mapo Reply
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get the best Algebra and trigonometry course in your pocket!

Source:  OpenStax, Itse 1359 introduction to scripting languages: python. OpenStax CNX. Jan 22, 2016 Download for free at https://legacy.cnx.org/content/col11713/1.32
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Itse 1359 introduction to scripting languages: python' conversation and receive update notifications?