Female Elf
There are many ways to combine separate pieces of information into a single line, some more efficient than others. Two adjacent strings (not separated by commas) will automatically be concatenated, but this doesn’t work for variables.
The following line
This approach doesn’t work for variables because writing two adjacent strings is just a different way of writing a single string. In other words, you can’t write a string using a string and a variable together like this.
Using Quotes
Character is the term used to describe a single letter, number, or punctuation mark. Strings of character intended to be displayed as text are known as string literals or just string. If you want to sell the interpreter that a block of text is meant to be displayed literally as text, you have to enclose the text in quotation marks; these can take several different forms:
“A text string enclosed by single quotes, all on one line again.’
’’’A text string including multiple lines of text
line breaks
and other formatting
can be enclosed in triple quotes.
’’’
“””or even:
triple double quotes
“””
‘Single and double-quoted lines can be continued on to the next line by placing a \ (backslash) at the end of the line.’ alert-warning
A value created using text in quotes will be given the type str (string)
Nesting Quotes
Sometimes, you will want to include literal quotation marks within your text. It is possible to nest quotes. That is, have one set of quotation marks inside another, so long as you use a different sort of quotation mark, like this:
In this instance, the interpreter will assume that it has reached the end of the text when it hits the second double quote”, so the substring ‘the rules’ is considered to be part of the main string including the single quotes/ this way, you can only have one level of nested quotes. Inside triple quotes, “”” or ‘’’, it is possible to use both normal single and double quotes without confusing things; the interpreter will wait until the second set of triple quotes before it decides that the string has come to an end. The best way to understand how they work is to experiment with assigning and printing out lots of different sorts of strings.
… #===(“)===(*)===#===(“)===#
… Egregious Response Generator
…Version ‘0.1’
…”FiliBuster” technologies inc.
…#===(“)===#===(“)===#
…”””
>>> print(boilerplate)
#==(“)===#===(*)===#===(“)===#
Egregious Response Generator
Version ‘0.1’
“FiliBuster” technologies inc.
#===(“)===#===(*)===#===(“)===# code-box
This can be very useful if you want to format a whole page or block of text. I’ll be looking at more sophisticated methods of string formatting.
Escaping Sequences
There will be times when nesting different types of quotes will not be sufficient; in these cases, you can include further literal quotation marks by escaping them, so \’ or \”. If you need to print a literal backslash, you have to escape the backslash itself, like this: \\.
The input () function, which I used to get user input in hello_word.py, stores the string with extra escapes where necessary, so the string will print out exactly the way it was typed.
Type something in: String, with lot\’s of \bad\ punct-uation (in it);
>>> variable
“String, with lot\\’s of \\bad\\ punct-uation (in it);”
>> print(variable)
String, with lot\’s of \bad\ punc-uation (in-it); code-box
Using Special Whitespace Characters
It is also possible to specify whitespace characters using a character sequence that begins with a backslash, as shown in Table 3-1. \n produces a linefeed (LF) character, this is ubstly different from the \e carriage return (CR): On a mechanical typewriter, the linefeed would just give you a new line, while the carriage return would properly start a new paragraph. One of the instances where you might need to know the difference is translating text files from one operating system to another: OS X uses the CR character at the end of lines. Whereas Unix and Linux use the LF character, and Windows uses both.
The meaning and usage of some of these sequences is somewhat lost in the mists of time. Mostly, you’ll want to use the \n escape/ the other most useful one is \t, which produces a tab character that can be useful for quick-and-dirty indentation of text. Most of the other whitespace character are only likely to be useful in very specialized situations (see bottom).
Sequence
Meaning
\n
Newline (LF)
\r
Carriage return (CR)
\t
Tab
\v
Vertical tab (VT)
\e
Escape character (ESC)
\f
Form feed (FF)
\b
Backspace
\a
Bell (BEL)
The following example allows you to nicely format your output for the screen:
iNGLuiDniNG Guinu uYta mu.
ReplyDeletePost a Comment