<< 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?
Pinias
how can black market be occurred when price ceiling is introduced?
Pinias
Is it necessary to make decision when it fails you
Evelin Reply
Pls when what fails u
MP
I think so
Kini
well i might naught know what you on about but i gotta tell you, it is necessary
Troy
yep
Ibe
Kk
MP
yep
Ibe
how can the demand side approach solve unemployment
Tshepiso Reply
demand side approach to solve unemployment
Tshepiso Reply
no get ur questions
Ibe
differentiate between choice and scale of preference
ALFRED Reply
choice are the various wants of every individual whiles scale of preference is the list of unsatisfied wants arranged in order of important priority
Kini
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
Ebenezer
four cardinal types of demand
Henry Reply
what are the types of demand
Henry
they are the derived,complementary,substitute and supplementary demands
Kini
What is equilibrium
Khan
hi
Ayesha
hello
Kini
equilibrium is the state of balance where there is no tendency for a change to occur.
Kini
Derived, competitive, complementary and composite demands
Aziz
Hi
MP
why is economics said to be a dismal science?
Kini
hi friends
Ibe
hello
Owusu
fyn
Ibe
fyn what is meaning of monopoly
Ibe
monopoly is when a particular good is produced without competing with any other good.
Ebenezer
good evening
Tweneboah
draw the market demand curve from the individual demand curve?
Shilpa
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
Clement
the total number of people at a given area or country
Clement
total number of people in a given area or country
okhiria
What is price Elasticity of demand?.
Samuel
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.
EMMANUEL
why is it that the long run Average cost doesn't touch the short run cost curve at its minimum?
Baah
what is supply
Precious
please rice and beans will be what type of demand but note we mostly cook it together
Oladosu Reply
complementary or joint demand
Yillah
but did you know you can demand for rice without beans so how is it joint or complementary
Oladosu
From my point of view, rice and beans are replaceable goods, hence they can't be complementary.
Carlos
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.
Carlos
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.
Akinlo
money is recognisable note to accept both parties selling and buying
Hassan
i don still understan
Rene
hey
Abdul
hi
Rene
money is anything generally accepted as a medium of exchange
Awwal
Money is anything generally accepted as a medium of exchange and for the settlement of goods and services .
Korda
hi good ppl, pls help out
Tumi
discuss human and natural resources as develop strategies ro improving living condition of citizens in developing countries.
Tumi
I don't understand the question.
Naomi
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?
Naomi
money is a legal thunder generally accepted as a medium of exchange for the payment of debt ,goods and services
Naomi
money is a way of payment.
Carlos
money is any thing that is generally accepted as a medium of exchange good for good and settlement of debt and means of payment
Yillah
money is nothing but a object which is used for exchange of goods and services.
Harshita
money is anything that is generally accepted as payment of goods and services and settlement of debt
Rebecca
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?

Ask