Lesson 1 ❮ Lesson List ❮ Top Page
❯ 1.2 Strings
⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺
EXPECTED COMPLETION TIME
❲▹❳ Video 7m 36s
☷ Interactive readings 5m
A string is a series of characters. Anything inside quotes is considered as a string.
You can use single (' ') or double quotes (" ") for strings.
It can be useful to use a variable inside a string, like to print your name in "Hello my name is {yourname}".
To do that, put the letter f right before the opening " ". Then, put braces { } around the variable you want to use.
This method is called f-string.
Let's perform some action to the string. A method is an action that Python can do on a piece of data, written after a period '.'.
Here are some basic string methods:
lower()
changes a string into the lower case.
upper()
changes the string into the upper case.
title()
changes the first character of each word to upper case.
Instead of using spaces, you can use \t for tab and \n for newline inside a string.
Using \ at the end of line allows you to continue writing the string into a new line. Try to delete the \ and see what happens.
One common error when working with string is related to the misuse of quotes. Quotes symbols is a part of the syntax.
The code gives a SyntaxError: EOL (end of line) while scanning string literal because the two quotes are not the same.
Change the last quote into a double quote (") to resolve this error.