Table of Contents
Python consists of a built-in string function and will be referred to as ”str” with handy features. Either single or double quotation marks enclose strings in Python. Read this article to learn more about Python String.
What is Python String?
The double or single quotes enclose this String literal. However, single quotes will be mostly used. Whereas, the double-quoted string has single quotes without any doubt. Single quoted string has double quotes.
Thus, string literals have multiple lines and require a backlash at the end of each line to escape the new line. String literal has triple quotes,””” or” that has multiple lines of text.
# Python string example
my_string = "Hello, World!"
# Output the original string
print("Original String:", my_string)
# Get the length of the string
length_of_string = len(my_string)
print("Length of String:", length_of_string)
# Convert the string to uppercase
uppercase_string = my_string.upper()
print("Uppercase String:", uppercase_string)
# Check if the string contains a specific substring
contains_substring = "World" in my_string
print("Contains 'World':", contains_substring)
Output
Original String: Hello, World!
Length of String: 13
Uppercase String: HELLO, WORLD!
Contains 'World': True
Assign String to a Variable
Assigning the string to the variable with the equal sign and the string. Let us look into the example given below:
Example
# Assigning a string to a variable
my_string = "Hello, Python!"
# Output the variable value
print("My String:", my_string)
Output
My String: Hello, Python!
What is Multiline Strings?
The multiline string will allow you to assign the multiline string to the variable with the three quotes.
# Multiline string example
multiline_string = '''
This is a multiline
string in Python.
It can span multiple lines.
'''
# Output the multiline string
print("Multiline String:")
print(multiline_string)
Output
Multiline String:
This is a multiline
string in Python.
It can span multiple lines.
Strings are Array
Strings in Python are considered as an array of bytes that will represent the Unicode characters. Hence, python won’t have a character data type. A single character has a string with a length of 1.
So, square brackets have the elements of the string.
Example
# String as an array example
my_string = "Hello, Python!"
# Accessing individual characters using indexing
first_char = my_string[0]
second_char = my_string[1]
# Output the characters
print("First Character:", first_char)
print("Second Character:", second_char)
# Iterating through the string as if it's an array
print("Iterating through the string:")
for char in my_string:
print(char)
Output
First Character: H
Second Character: e
Iterating through the string:
H
e
l
l
o
,
P
y
t
h
o
n
!
Looping Through a String
Strings are arrays that will help to loop through the characters in the string and with a for loop.
# Looping through a string example
my_string = "Hello, Python!"
# Iterating through the string using a for loop
print("Iterating through the string:")
for char in my_string:
print(char)
Output
Iterating through the string:
H
e
l
l
o
,
P
y
t
h
o
n
!
What is String Length in Python?
In Python, the length of the string can be evaluated using the len() function.
Example
# String length example
my_string = "Hello, Python!"
# Get the length of the string
length_of_string = len(my_string)
# Output the length of the string
print("Length of the String:", length_of_string)
Output
Length of the String: 14
Check String
Python allows you to check a certain phase or character in the string. Hence, it uses the keyword ‘in‘
Example
# String checking example
my_string = "Hello, Python!"
# Check if the string contains a specific substring
substring_to_check = "Python"
if substring_to_check in my_string:
print(f"The string '{my_string}' contains the substring '{substring_to_check}'.")
else:
print(f"The string '{my_string}' does not contain the substring '{substring_to_check}'.")
Output
The string 'Hello, Python!' contains the substring 'Python'.
Using the if statement in the above code.
# String checking with if statement example
my_string = "Hello, Python!"
# Check if the string contains a specific substring using if statement
substring_to_check = "Python"
if substring_to_check in my_string:
print(f"The string '{my_string}' contains the substring '{substring_to_check}'.")
else:
print(f"The string '{my_string}' does not contain the substring '{substring_to_check}'.")
Output
The string 'Hello, Python!' contains the substring 'Python'.
What is Check if NOT in Python?
Python allows you to find if a certain phrase or character doesn’t exist in the string.
Example
# String checking with 'not in' example
my_string = "Hello, Python!"
# Check if the string does not contain a specific substring using 'not in'
substring_to_check = "Java"
if substring_to_check not in my_string:
print(f"The string '{my_string}' does not contain the substring '{substring_to_check}'.")
else:
print(f"The string '{my_string}' contains the substring '{substring_to_check}'.")
Output
The string 'Hello, Python!' does not contain the substring 'Java'.
Let us look at this example using the if statement.
# String checking with if statement example
my_string = "Hello, Python!"
# Check if the string does not contain a specific substring using if statement
substring_to_check = "Java"
if substring_to_check not in my_string:
print(f"The string '{my_string}' does not contain the substring '{substring_to_check}'.")
else:
print(f"The string '{my_string}' contains the substring '{substring_to_check}'.")
Output
The string 'Hello, Python!' does not contain the substring 'Java'.
Conclusion
To conclude, string operations are crucial for effective text manipulation in Python. These skills serve as a foundation for more advanced programming concepts and are valuable for several applications.
Python String- FAQs
Q1.What is a string in Python?
Ans. A string in Python consists of a sequence of characters.
Q2. What is to string in Python?
Ans. The tostring() refers to a method that will turn other data types into the string.
Q3. What does __ str __ do in Python?
Ans. The __str__() method will return the human-readable or informal string representation of an object.
Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together