….
Python Strings – Mastering Text in Python
In Python, strings are one of the most important data types, used to represent and manipulate text. Whether you’re building websites, analyzing data, or developing AI applications, working with strings is an essential skill.
A string is simply a sequence of characters enclosed within single quotes (‘ ‘), double quotes (” “), or even triple quotes (”’ ”’ / “”” “””) for multi-line text.
print("Hello, World!")
print('Python is amazing!')
Both single and double quotes work the same way in Python.
Quotes Inside Strings
When your text contains quotes, Python allows flexible usage of single and double quotation marks.
print("It's a great day!")
print('He is called "Johnny"')
print("She said, 'Hello!'")
Assigning Strings to Variables
Strings can be stored in variables for reuse.
message = "Welcome to GoNimbus!"
print(message)
Multiline Strings
For longer messages, docstrings, or formatted text, you can use triple quotes.
poem = """Roses are red,
Violets are blue,
Python is simple,
And powerful too!"""
print(poem)
Triple quotes preserve line breaks and spacing exactly as written.
Strings are Like Arrays
In Python, a string behaves like a sequence of characters. Each character has an index starting from 0
.
word = "Python"
print(word[0]) # P
print(word[5]) # n
⚡ Note: Python does not have a separate character type. A single character is just a string of length 1.
Looping Through Strings
You can loop through characters in a string using a for
loop:
for letter in "GoNimbus":
print(letter)
This prints each character one by one.
String Length
The len()
function returns the number of characters in a string.
text = "Hello, Python!"
print(len(text)) # 14
Checking Substrings (Membership Operators)
- Check if a string exists inside another string:
quote = "The best things in life are free!"
print("free" in quote) # True
- Using in an if statement:
if "best" in quote:
print("Yes, 'best' is present!")
- Check if NOT present:
print("expensive" not in quote) # True
Advanced String Operations 🚀
🔹 String Concatenation
You can join two or more strings using the +
operator.
first = "Go"
second = "Nimbus"
result = first + second
print(result) # GoNimbus
🔹 String Repetition
Multiply a string using the *
operator.
print("Hi! " * 3)
# Output: Hi! Hi! Hi!
🔹 String Slicing
Extract parts of a string using slicing ([start:end]
).
text = "GoNimbus"
print(text[0:2]) # Go
print(text[2:]) # Nimbus
print(text[-3:]) # bus
🔹 String Methods (Built-in Functions)
Python provides many helpful string methods:
name = "gOnImBuS"
print(name.upper()) # GONIMBUS
print(name.lower()) # gonimbus
print(name.title()) # Gonimbus
print(name.strip()) # removes spaces
print(name.replace("Go", "Pro")) # ProNimbus
print("Nimbus" in name) # True
Best Practices for Strings
✔️ Use f-strings for clean and efficient formatting:
name = "Python"
print(f"Welcome to {name} programming!")
✔️ Use triple quotes for long texts and docstrings.
✔️ Always validate user input strings to prevent errors.
✔️ Explore string methods like .find()
, .split()
, .join()
, and .startswith()
for powerful text manipulation.
Quick Recap
- Strings are enclosed in
' '
or" "
. - Triple quotes are used for multi-line text.
- Strings behave like arrays → indexing & slicing apply.
- Use
in
andnot in
to check for substrings. - Built-in string methods make text processing powerful and easy.
👉 At GoNimbus, we make Python simple and practical. Strings are just the beginning — once you master them, you unlock the key to text processing, data analysis, and automation.