What are Strings in Python? Examples & Explanation

What are Strings in Python? Examples & Explanation

What are Strings in Python? Examples & Explanation

What is a String in Python?

A string is a sequence of characters, where each character can be an alphabet, a number, a special character, etc. Python uses Unicode to represent characters, which means it can handle a vast array of symbols and alphabets from various languages.

Why Unicode Over ASCII?

While ASCII encodes a limited set of characters, Unicode provides a unique value for every possible character in every language. With modern applications requiring support for diverse symbols and languages, Unicode is the superior choice.

Key Features of Strings in Python

  • Strings in Python are stored as arrays of bytes representing Unicode characters.
  • Strings support both forward (0, 1, 2, …) and backward (-1, -2, -3, …) indexing.
  • Python does not have a character data type; even a single character is considered a string of length 1.

Working with Strings in Python

Initializing/Assigning Strings

You can assign strings using single, double, or triple quotes.
# Assigning using single quotes
string1 = 'Welcome'

# Assigning using double quotes
string2 = "to"

# Assigning using triple quotes
string3 = '''FACE Prep's Python Programming Tutorial'''

print(string1)
print(string2)
print(string3)

Output:

Welcome
to
FACE Prep's Python Programming Tutorial

Handling Quotes Inside Strings

When a string contains quotes, use the opposite type of quotes or escape sequences.
# Using double quotes for a string containing single quotes
string1 = "Welcome to 'Python' course"
print(string1)

Output:

Welcome to 'Python' course
For multiline strings, use triple quotes:
string1 = """Welcome
to
FACE Prep's Python Programming Tutorial"""
print(string1)

Output:

Welcome
to
FACE Prep's Python Programming Tutorial

Accessing Characters in a String

Single Characters

Access single characters using indices.
string1 = "FACE Prep's Python Tutorials"
print(string1[1])  # Prints 'A'
print(string1[-1])  # Prints 's'

Output:

A
s

Slicing Multiple Characters

Use slicing syntax string[start:end] to access parts of a string.
print(string1[12:18])  # Prints 'Python'
print(string1[-12:-2])  # Prints 'Tutorial'

Output:

Python
Tutorial

Modifying Strings

Strings in Python are immutable. You cannot modify them directly but can reassign a new value.
string1 = "FACE Prep"
string1 = "New FACE Prep"
print(string1)

# Deleting a string
string2 = "Python Tutorials"
del string2
# print(string2)  # Raises NameError

Output:

New FACE Prep
Traceback (most recent call last):
NameError: name 'string2' is not defined

Escape Sequences

Escape sequences start with a backslash () to handle special cases like quotes or newlines.
string1 = 'I\'m in love with Python Language'
print(string1)

Output:

I'm in love with Python Language
Raw strings (r or R) ignore escape sequences:
string1 = R'I prefer learning Python\Java'
print(string1)

Output:

I prefer learning Python\Java

String Operations

Concatenation

a = "Hello"
b = "World"
print(a + " " + b)

Repetition

print(a * 3)  # Prints 'HelloHelloHello'

Membership

print("World" in b)  # True
print("Python" not in b)  # True

String Formatting

Use the format() method for inserting variables into strings.
a = 5
b = "My age is {}"
print(b.format(a))
For multiple values:
c = "My name is {0} and my age is {1}."
print(c.format("John", 30))

Output:

My name is John and my age is 30.

String Methods

Python provides various built-in methods for string manipulation. Here are a few:
MethodDescription
upper()Converts string to uppercase
lower()Converts string to lowercase
strip()Removes leading/trailing spaces
replace(a, b)Replaces a with b
split()Splits string into a list
join()Joins elements of a list into a string
string = " Python Programming "
print(string.upper())
print(string.strip())
print(string.replace("Programming", "Language"))

Output:

PYTHON PROGRAMMING
Python Programming
Python Language

Conclusion

Strings in Python are an essential data type used to store and manipulate textual data. They are immutable, meaning their values cannot be changed after creation, which ensures data integrity. Python provides various methods and operations to work with strings, making it easy to perform tasks such as concatenation, slicing, formatting, and searching. Understanding how to handle strings effectively will enable you to process and manipulate text in a wide range of applications, from basic data handling to complex text processing tasks.Click here to know more our program!
c