Strings in Python
Strings in Python are sequences of characters, enclosed in either single (' ') or double (" ") quotes. They support various operations such as concatenation, slicing, and formatting.
Creation: Strings can be created using single or double quotes:
name = 'John'
ormessage = "Hello, World!"
.Indexing: Characters within a string can be accessed using index notation:
print(message[0])
outputs 'H'.Slicing: Portions of a string can be extracted using slicing notation:
print(message[0:5])
outputs 'Hello'.Concatenation: Strings can be combined using the
+
operator:greeting = "Hello, " + name
.Formatting: Strings support placeholders for dynamic content:
formatted = f"Hello, {name}"
.Methods: Python provides built-in methods for string manipulation, like
upper()
,lower()
,split()
,strip()
, and more.Immutability: Strings are immutable, meaning their values cannot be changed after creation. However, new strings can be created from existing ones.
Strings are fundamental data types in Python, extensively used for text processing, manipulation, and representation.
Subscribe to my newsletter
Read articles from Shaique Hossain directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by