Lecture 9 Getting Started With Python 2021
Lecture 9 Getting Started With Python 2021
You can also assign a variable letters to the string and write a code as below
Multi Line Strings
One way is to break the string up across multiple lines and put a backslash (\) at
the end of all but the last line.
String Concatenation
This is when two or more strings are joined together
String Concatenation
The same concatenation can be used to join two related strings such as joining a
first name and a last name into a full name(as shown below)
String Indexing
Each character in a string has a numbered position called an index. You can
access the character at the nth position by putting the number n between two
square brackets ([]) immediately after the string:
Take note of the error message due to wrong spelling. flavor[1] returns the
character at position 1 in "fig pie", which is i. Wait. Isn’t f the first character of
"fig pie"? In Python—and in most other programming languages—counting
always starts at zero. To get the character at the beginning of a string, you need to
access the character at position 0:
String Indexing
The following figure shows the index for each character of the string “fig pie”:
String Indexing
Strings also support negative indices:
The last character in a string has index -1, which for "fig pie" is the letter e. The
second to last character i has index -2, and so on. The following figure shows the
negative index for each character in the string "fig pie":
Assignment 5
Print a string that uses double quotation marks inside the string
Print a string that spans multiple lines
Create a string and print its length using len()
Print two strings, concatenate them, and print the resulting string