Professional Documents
Culture Documents
Working With Data
Working With Data
DATA
The sentence seems to look fine, but Python will show you an error message
«EOL while scanning string literal».
Why did that error occur? The abbreviation "EOL" stands for "End-of-line" and it
means that Python went through the text and didn't find the end of the string. The
sentence was divided into two parts. The first one is "That" and the second one
is "s my car".
ESCAPE SEQUENCES
A backslash can be used in a combination with another symbol to do some
particular things.
For example, if you use \b in the middle of the string and then try to print it,
you won't see the character before \b. The combination \b means a
backspace character:
4
ESCAPE SEQUENCES
Now if you want to print text that contains \, you can double it. For example, this is
useful when you need to print literally \n, because print('\n') will only output a new
blank line. Double backslash will help you in such situations!
When we want to know the length of a string, escape sequences are also taken into
account:
Pitch deck
WHAT IS AN ESCAPE SEQUENCE?
• The \n combination starts a new line, so you will see the following output:
• The next example shows the escape sequence \t. As it was said above, \t is used for tabulation. If you put it in the middle of a string,
the two parts of the string will be divided by some blank space that is called tabulation. It is quite useful when you work with a text.
• Another escape sequence that can be useful while you are working with text is \r. The common name for this escape sequence is a
carriage return. It moves characters after \r to the beginning of the line, replacing the exact number of old characters. That is, if the
length of the string is longer before this escape sequence, then only the required number of characters is rewritten.
OTHER EXAMPLES
• Let's consider an example with the escape sequence \n
• The \n combination starts a new line, so you will see the following output:
• The next example shows the escape sequence \t. As it was said above, \t is used for tabulation. If you put it in the middle of a string,
the two parts of the string will be divided by some blank space that is called tabulation. It is quite useful when you work with a text.
• Another escape sequence that can be useful while you are working with text is \r. The common name for this escape sequence is a
carriage return. It moves characters after \r to the beginning of the line, replacing the exact number of old characters. That is, if the
length of the string is longer before this escape sequence, then only the required number of characters is rewritten.
OTHER EXAMPLES
• Please note that the string length remains the same!
• Escape sequences are simple to use, aren't they? Let's talk more about the length of strings. For example:
• After calling the len() function, we can see that the length of the string with an escape sequence (in this case \n) is greater.
• Be careful while working with strings because the function print() doesn't show escape sequences.
EXERCISE
• If the separator doesn't occur in the string, then the result of the method is a list with the original string as its only
element:
• It's pretty efficient when you know the exact number of input values. In case you don't, it's likely to result
in ValueError with a message telling you either that there are too many values to unpack or not enough of them.
JOIN A LIST
• Note that this method only works if the elements in the iterable object are strings. If, for example, you want to create a
string of integers, it will not work. In this case, you need to convert the integers into strings explicitly or just work
with strings right from the outset.
SPLIT MULTIPLE LINES
• The splitlines() method is similar to split(), but it is used specifically to split the string by the line boundaries.
• There are many escape sequences that signify the end of the line, but the split() method can only take one separator.
So this is where the splitlines() method comes in handy:
SPLIT MULTIPLE LINES
• The method has an optional argument keepends that has a True or False value. If keepends = True linebreaks are
included in the resulting list:
• You can also use several string methods at once. It is called chaining, and it works because most of the string methods
return a copy of the original string:
EXERCISE
• There are certain situations when you want to make your strings kind of "dynamic", i.e. make them
change depending on the value of a variable or expression.
• For example, you want to prompt the user for their name and print the greeting to them with the
name they entered. But how can you embed a variable into a string? If you do it in the most intuitive
way, you'll be disappointed:
• Luckily, Python offers a large variety of methods to format the output the way you want and
we'll concentrate on the main two:
• Formatted string literals
• The str.format() method
• Earlier the % operator was in use. This built-in operator was derived from C-language and was
used in some situations in the following way:
• Thus, the variable to the right of % was included in the string to the left of it. If we'd wanted to
divide 11 by 3, the / operator would have returned a float number with a lot of decimal places.
STRING FORMATTING
• With % character, we could control the number of decimal places, for example, reduce their
number to 3 or 2:
• For every operation, you had to know plenty of specifiers, length modifiers and conversion
types. A huge variety of extra operators led to some common errors. That's why more modern
and easy to use operators were introduced. Progress never stops, you know!
• Formatting your strings also makes your code look more readable and easily editable.
STRING FORMATTING
• Python uses C-style string formatting to create new, formatted strings. The "%" operator is
used to format a set of variables enclosed in a "tuple" (a fixed size list), together with a
format string, which contains normal text together with "argument specifiers", special
symbols like "%s" and "%d".
• Let's say you have a variable called "name" with your user name in it, and you would then
like to print(out a greeting to that user.)
• The operation of the method is already described in its name: in the string part, we introduce
curly braces as placeholders for variables enlisted in the format part:
• The expressions are put instead of braces in the order they were mentioned:
str.format() method
• You can use the same variable in one string more than once if it's necessary. Furthermore, you
can address the objects by referring to their positions in curly braces (as usual, the numbering
starts from zero). Attention: the order you choose can be very important. The following two
codes:
• And
• Note that you can also mix the order as you want if you use keywords. Here's the formatted string:
• The last code snippet resulted in SyntaxError, since positional arguments are to be mentioned first.
Formatted String Literals
• Formatted string literals (or, simply, f-strings) are used to embed the values of expressions inside
string literals. This way is supposed to be the easiest one: you only need to put f before the string and
put the variables you want to embed into the string in curly braces. They are also the newest feature
among all string formatting methods in Python.
• If you print this short string, you'll see an output that is five times longer than its representation in
code:
Formatted String Literals
• You can also use different formatting specifications with f-literals, for example rounding decimals
would look like this:
Exercise
What will be the output of this code?
Exercise
How many decimal places will this code return?
2
(ASSIGNMENT) – you can discuss with your classmates
• The output should be user-friendly, but the code part is also important. Well-structured and readable code is very important for being a
good programmer. Now it's up to you to decide, which formatting method to choose.
• Imagine you need to compose a dynamic URL for every certain user with user-specific details. Suppose, you want to send different
URLs for every user, depending on their name and profession. The base would be something like
• "http://example.com/*nickname*/desirable/*profession*/profile", where nickname and profession are prompts from a user and are
dynamic.
• Read the nickname and profession of the user from the input and print a user-specific URL. Don't bother about any rules of composing
the URLs and just use raw input to accomplish the task.
HO1_3SECTIONCLASSNUMBER