πŸ”€ Python String Basics β€” Working With Text in Python

Introduction 🌟

Strings are one of the most essential data types in Python. A string represents a sequence of characters used to store and manipulate text. Whether you're printing messages, handling user input, or processing data β€” strings are everywhere!

Note

πŸ’‘ Strings in Python are written inside quotes β€” single, double, or triple quotes.

1. Creating Strings ✍️

You can create strings in multiple ways:

create_strings.py

s1 = "Hello"
s2 = 'Python'
s3 = """This is
a multi-line string"""

print(s1)
print(s2)
print(s3)
  • Single quotes β†’ 'Hello'
  • Double quotes β†’ "Hello"
  • Triple quotes β†’ for multi-line text

2. String Indexing 🎯

Each character in a string has an index (position).

indexing.py

text = "Python"
print(text[0])  # P
print(text[3])  # h
print(text[-1]) # n (last character)

Note

🧠 Indexing starts from 0. Negative indexing starts from the end.

3. String Slicing βœ‚οΈ

Extract parts of a string using slicing.

slicing.py

word = "Python"
print(word[0:3])   # Pyt
print(word[2:5])   # tho
print(word[:4])    # Pyth
print(word[2:])    # thon
print(word[-3:])   # hon

4. String Length πŸ“

Use len() to find the number of characters in a string.

length.py

msg = "Hello Python"
print(len(msg))

5. String Concatenation βž•

Join two or more strings using the + operator.

concat.py

first = "Hello"
second = "World"
print(first + " " + second)

Note

πŸ’‘ Python does not add spaces automatically β€” you must add them manually.

6. String Repetition πŸ”

repeat.py

print("Hi! " * 3)

7. Escape Characters πŸ”§

Use backslashes to include special characters inside strings.

Escape CodeDescriptionExample
\nNew Lineprint("Hello\nWorld")
\tTab Spaceprint("A\tB")
\'Single Quoteprint('It\\'s Python')
\"Double Quoteprint("He said \\"Hi\\"")
\\Backslashprint("C:\\\\Path")

escape_examples.py

print("Hello\nWorld")
print("He said \"Hi\"")

8. String Methods 🧰

Python provides powerful built-in methods for strings.

string_methods.py

msg = "hello python"

print(msg.upper())      # HELLO PYTHON
print(msg.lower())      # hello python
print(msg.title())      # Hello Python
print(msg.capitalize()) # Hello python
print(msg.replace("python", "world"))
print(msg.split())      # ['hello', 'python']

Note

🧼 String methods do not change the original string (immutable).

9. Checking Substrings πŸ”

substring.py

text = "Python programming"
print("Python" in text)    # True
print("Java" in text)      # False

10. f-Strings (String Formatting) ✨

f-Strings allow you to embed variables directly inside strings.

fstring.py

name = "Sathish"
age = 25
print(f"My name is {name} and I am {age} years old.")

Note

⚑ f-Strings are the cleanest and fastest way to format strings.

11. Multiline Strings 🧡

multiline.py

msg = """This is
a multiline
string in Python."""
print(msg)

12. Real-World Example 🌍

real_world_example.py

name = input("Enter your name: ")
greeting = f"Hello {name}, welcome to Python!"
print(greeting.upper())

Conclusion πŸŽ‰

>>β€œStrings are the storytellers of your program β€” master them, and your code becomes expressive.” ✨

You're now ready to explore deeper string topics like slicing, formatting, methods, or even regex! Just tell me the next topic you want. 😊