Introduction to Python Strings
Python strings are the collection of characters. Python stores the string as an ordered sequence, and we can access any element in the sequence using index positions.
![Python Strings](https://www.logicalpython.com/wp-content/uploads/2023/12/image-31.png)
We can use single quotes or double quotes to create a string. Either we use single quote or double quote, both mean one and the same thing.
String with double quote.
print("Hello")
String with single quote.
print('String in a single quote')
This string will results in an error because “I’m” ended the string. To fix this type of error we can use double quotes(“”).
print('I'm trying to create an error')
Now we won’t see any error.
print("I'm trying to fix an error")
Multiline String
We can create a multiline string using the triple quotes. It can be single triple quotes or double triple quotes. It will also retain the tabs and whitespace.
Double triple quotes.
greeting = """
Hello,
How are you.
Thanks for visiting.
"""
print(greeting)
Single triple quotes.
greeting = '''
Hello,
How are you.
Thanks for visiting.
'''
print(greeting)
String Immutability
Strings are immutable, which means once we assign a value to a string variable, we cannot replace a specific single character of the string. We can reassign the whole string, but cannot replace a single character.
name = 'Peter Omith'
print(name[6])
Now we want to replace ‘O’ with ‘S’ using its index position. But we cannot do that because of its immutability.
name[6] = 'S'
But, we can reassign the whole string as:
name = 'Peter Smith'
print(name)
Concatenating Strings
Using the + operator, we can concatenate two strings. While we are concatenating the strings with the plus operator, note that it is not adding any white space in between.
first_name = "Logical"
last_name = "Python"
name = first_name + " " + last_name
print(name)
Check if a sub-string exists – Membership Test
To check whether the sub-string exists in the sting or not, we use the in operator. If we use not with in, we can check the vice versa as well. It will check if the string is not present in the string.
Check if a sub-string exists.
name = "Logical Python"
if "Python" in name:
print("Welcome to Python Tutorial")
else:
print("Welcome to Java Tutorial")
Check if a sub-string does not exists.
name = "Logical Python"
if "Python" not in name:
print("Welcome to Python Tutorial")
else:
print("Welcome to Java Tutorial")
String Functions
Python has a few built-in functions that can be used with the Strings. Let us go through them:
![](https://www.logicalpython.com/wp-content/uploads/2023/12/image-34.png)
Brief of each:
- len() – Simply returns the number of characters in the String.
- char() – Accepts integer as an input and converts the integer to String Character.
- ord() – This accepts a string character as an input and converts it into an integer.
Here are the examples of each of them:
len()
str = "Logical Python"
print(len(str))
str = "Logical Python"
print(len(str))
ord()
print(ord('A'))
print(ord('a'))
chr()
print(chr(65))
print(chr(97))
Resources
These are the Free Notes shared with the YouTube Video.
**Note: All links are Password Protected. For the PASSWORD, please follow the steps in the DESCRIPTION of the respective Video.
Linked video : https://youtu.be/9SqnFc1CN90