Welcome to the lesson on Python Strings
Strings in python are surrounded by either single quotation marks, or double quotation marks.
'hello' is the same as "hello"
You can display a string literal by using print()
Some Examples:
Assigning String to a variable
Assigning a string to a variable is done with the variable name followed by an equal sign and the string:
Some Examples:
Multiline Strings
You can assign a multiline string to a variable by using three quotes:
Some Examples: three double quotes
Some Examples: three single quotes
Strings are Arrays
strings in Python are arrays of bytes representing unicode characters.
Some Examples: Get the character at position 7 (remember that the first character has the position 0):
Looping Through a String
Since strings are arrays, we can loop through the characters in a string, with a for loop.
Some Examples: Loop through the letters in the word "Grape":
String Length
To get the length of a string, use the len() function.
Some Examples: The len() function returns the length of a string:
Check String
To check if a certain phrase or character is present in a string, we can use the keyword in
Some Examples: Check if "movie" is present in the following text:
Some Examples: Using an if statement:
Check string not in a variable
To check if a certain phrase or character is NOT present in a string, we can use the keyword not in
Some Examples: Check if "Singer" is NOT present in the following text:
Some Examples: Using an if statement:
End of Python Strings
You have learned Python Strings in simple terms. Let's proceed on to Slicing Strings.