Printing and manipulating text in Python - AkuCode

Printing and manipulating text in Python

    In hello_world.py you learned how to get some basic output by using the print() function. You can use it to print out a literal string of characters or the value of a variable. Normally, each print statement starts on a new line, but it is possible to print several values on a single line by separating them with commas; print() will then concatenate them into a single line, separated by spaces. In hello_world.py you learned how to get some basic output by using the print() function. You can use it to print out a literal string of characters or the value of a variable. Normally, each print statement starts on a new line, but it is possible to print several values on a single line by separating them with commas; print() will then concatenate them into a single line, separated by spaces.
>>>  Race = “Elf”
>>> Gender = “Female”
  >>> print(Gender, Race) 

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.
>>> print(“Male” “Gnome”) 
Will give the output
  MaleGnome
    The following line
>>> print (“Male” Race)
Result in this
	File “”, line 1
		Print(“Male” Race)
  SyntaxError: invalid ^syntax
    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.
Note: it is possible to join strings together using the + sign. In the context of strings, this is called the concatenation operator; an operator is a command that often consists of a symbol or combination of symbols, placed between two variables, known as operands, just like you would write a mathematical equation. The situation is further complicated because the + sign is also used to add numbers together, as you will see in the next section (this is called overloading). Using the + sign to concatenate words can slow down the execution of your program to an alarming degree. For this reason, I recommended that you don’t use this method if you need to join lots of string; some more efficient ways of joining strings together.

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 double quotes and all on one line.”
“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.’
Note: The backslash \ escape the newline character, which means it tells the interpreter to ignore any special meaning that the next character might have. All the unescaped characters after that are treated normally.
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:
>>> text = “She quoted ‘the rules’ at him” 
    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.
>>> boilerplate = “””
… #===(“)===(*)===#===(“)===#
… Egregious Response Generator
…Version ‘0.1’
…”FiliBuster” technologies inc.
…#===(“)===#===(“)===#
…”””
>>> print(boilerplate)
#==(“)===#===(*)===#===(“)===#
Egregious Response Generator
Version ‘0.1’
“FiliBuster” technologies inc.
#===(“)===#===(*)===#===(“)===#
    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.
>>> variable = input (‘Type something in: ‘)
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);

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:
>>> print(“Example Heading\n\nFollowed by a new line\nor two of text\n \
… \tName\n\tRace\n\tGender\nDon\’t forget to escape \’\\\’.”
  ) 
  

Example Heading

Followed by a line or two of text Name Race Gender Don’t forget to escape ‘\’.
Comments
ChrystopherCDizon Comment Poster
iNGLuiDniNG Guinu uYta mu.