What is a String?
In Python, a string is a sequence of characters enclosed within either single quotes (') or double quotes ("). Strings are a fundamental data type and are used to represent text. For example the following code creates a variable called greeting which stores the string hello:
greeting = "hello"
greeting = "hello"
Check if two strings are the same
It is easy to check if two strings are the same. Just use the == operator as you would with two numbers:
name = "Bart"
another_name = "Lisa"
if( name == another_name):
print("names are the same")
else:
print("names are different")
You can also use != to check if the strings are different.
name = "Bart"
another_name = "Lisa"
if( name == another_name):
print("names are the same")
else:
print("names are different")
You can also use != to check if the strings are different.
Accessing one letter in the String
You can use a String like an array. This means you can access each letter separately.
For example the following code will display the first letter in the name variable. Since name stores "Bart" the letter "B" will be displayed:
name = "Bart"
print name[0]
The following code using iteration. It will display each letter from name on a seperate line:
Note: when using for i in range. i will start at 0 and increment by 1 for each loop.
name = "Bart"
for i in range(4):
print(name[i])
You can also access one letter at a time like this:
name = "Bart"
for x in name:
print(x)
For example the following code will display the first letter in the name variable. Since name stores "Bart" the letter "B" will be displayed:
name = "Bart"
print name[0]
The following code using iteration. It will display each letter from name on a seperate line:
Note: when using for i in range. i will start at 0 and increment by 1 for each loop.
name = "Bart"
for i in range(4):
print(name[i])
You can also access one letter at a time like this:
name = "Bart"
for x in name:
print(x)
Find: Checking if a letter exists in a word
In Python, the find() method is used to find the index of a letter ( or word) within a string. If the substring is not found, it returns -1.
For example the following code will store 10 in the variable index. This is because the word "simple" starts at index 10:
in the string sentence.
index = sentence.find("simple")
The following code will store -1 in the variable index. This is because the word "complex" is not in the string sentence.:
sentence = "This is another a simple example sentence."
index = sentence.find("complex")
Keep in mind that find() is case-sensitive. You can get around this by converting both the strings being compared to lowercase (or uppercase) using lower() or upper() before calling find().
For example the following code will store 10 in the variable index. This is because the word "simple" starts at index 10:
in the string sentence.
index = sentence.find("simple")
The following code will store -1 in the variable index. This is because the word "complex" is not in the string sentence.:
sentence = "This is another a simple example sentence."
index = sentence.find("complex")
Keep in mind that find() is case-sensitive. You can get around this by converting both the strings being compared to lowercase (or uppercase) using lower() or upper() before calling find().