Member-only story
Strings are one of the most fundamental data types in any programming language, including Python. They allow us to store and manipulate text-based data, making them essential for tasks such as input/output operations, file handling, and web development.
This article will introduce you to Python strings, their properties, methods, and how to use them effectively in your programs.
What is a String?
In Python, a string is a sequence of characters enclosed in quotes, either single (‘ ‘) or double (“ “) quotes. For example:
name = "John Doe"
age = '25'
address = "123 Main St."
String Properties
Python strings are immutable, meaning they cannot be changed once created. When we modify a string, Python creates a new string instead of modifying the existing one. Here’s an example:
str1 = "Hello"
str1[0] = 'h' # Throws error since strings are immutable
new_str = str1.lower() # Creates a new string with all lowercase letters
print(new_str) # Output: hello