Strings
To create a string in Python, wrap your text in quotation marks. Single and double quotes both work, so 'hello' and "hello" give you the very same value.
The print() function shows a string literal on the screen.
Example
Output: complete Python program
print("Hello")
print('Hello')
Quotes Inside Quotes
A string may contain quote characters, but they have to be a different kind from the ones wrapping the string.
Example
Output: complete Python program
print("It's alright")
print("He is called 'Johnny'")
print('He is called "Johnny"')
Assign String to a Variable
To store a string in a variable, write the variable's name, then an equal sign, then the string.
Example
Output: complete Python program
a = "Hello"
print(a)
Multiline Strings
Three quotes in a row let you store a string that spans several lines in a variable.
Note: The output keeps each line break exactly where it sits in your code.
Example
Three double quotes do the job:
Output: complete Python program
a = """Lorem ipsum dolor sit amet,
consectetur adipiscing elit,
sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua."""
print(a)
Example
So do three single quotes:
Output: complete Python program
a = '''Lorem ipsum dolor sit amet,
consectetur adipiscing elit,
sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua.'''
print(a)
Strings are Arrays
As in many other languages, a Python string is essentially an array of Unicode characters.
There is no separate type for a single character, though. One character is just a string whose length is 1.
To reach an element of a string, put its position in square brackets.
Example
Fetch position 1 from the string. Positions are counted from 0, so this gives us the second character:
Output: complete Python program
a = "Hello, World!"
print(a[1])
Looping Through a String
Because a string works like an array, a for loop can visit its characters one at a time.
Example
Print each letter of "banana" on its own line:
Output: complete Python program
for x in "banana":
print(x)
String Length
The built-in len() function tells you how long a string is.
Example
Here, len() counts the characters and gives back 13:
Output: complete Python program
a = "Hello, World!"
print(len(a))
Check String
The keyword in tells you whether a particular phrase or character appears somewhere inside a string.
Example
Look for "free" in this sentence:
Output: complete Python program
txt = "The best things in life are free!"
print("free" in txt)
Example
Combine it with if to print only when "free" is found:
Output: complete Python program
txt = "The best things in life are free!"
if "free" in txt:
print("Yes, 'free' is present.")
Check if NOT
To confirm that a phrase or character is absent from a string, use not in instead.
Example
Confirm that "expensive" does not appear in this sentence:
Output: complete Python program
txt = "The best things in life are free!"
print("expensive" not in txt)
Example
Print a message only when "expensive" is missing:
Output: complete Python program
txt = "The best things in life are free!"
if "expensive" not in txt:
print("No, 'expensive' is NOT present.")