DEV Community

Cover image for Day 5/100: Working with Strings — Basics to Formatting
 Rahul Gupta
Rahul Gupta

Posted on

Day 5/100: Working with Strings — Basics to Formatting

Welcome to Day 5 of the 100 Days of Python series!
Today we’re diving deep into one of the most commonly used data types in Python: strings.

Whether you’re building a chatbot, scraping websites, or working with data — you'll be using strings all the time. So let’s master the basics and learn how to format and manipulate strings effectively.


📦 What You'll Learn Today

  • What strings are and how to define them
  • String indexing and slicing
  • Common string methods
  • String concatenation and repetition
  • How to format strings cleanly

🔤 What Is a String?

In Python, a string is a sequence of characters surrounded by quotes.

name = "Alice" greeting = 'Hello, world!' 
Enter fullscreen mode Exit fullscreen mode

You can use either single (' ') or double (" ") quotes.


🧱 String Indexing and Slicing

Indexing:

Each character in a string has an index number:

word = "Python" print(word[0]) # P print(word[5]) # n 
Enter fullscreen mode Exit fullscreen mode

Python uses zero-based indexing, so the first character is at position 0.

Slicing:

You can extract parts of strings using slicing:

print(word[0:3]) # Pyt print(word[2:]) # thon print(word[-1]) # n (last character) 
Enter fullscreen mode Exit fullscreen mode

🔁 String Concatenation and Repetition

Concatenation:

Use + to join strings:

first = "Good" second = "Morning" print(first + " " + second) # Good Morning 
Enter fullscreen mode Exit fullscreen mode

Repetition:

Use * to repeat a string:

print("Ha" * 3) # HaHaHa 
Enter fullscreen mode Exit fullscreen mode

🧽 Common String Methods

Python strings come with lots of built-in methods:

text = " Hello, Python! " print(text.strip()) # Remove whitespace: "Hello, Python!" print(text.lower()) # Convert to lowercase print(text.upper()) # Convert to uppercase print(text.replace("Python", "World")) # Replace text print(text.find("Python")) # Find substring index 
Enter fullscreen mode Exit fullscreen mode

Some Useful String Methods:

Method Description
.strip() Removes leading/trailing whitespace
.lower() Converts to lowercase
.upper() Converts to uppercase
.replace() Replaces one substring with another
.find() Finds the first index of a substring
.split() Splits string into a list
.join() Joins list into a string

🧠 String Formatting

Let’s say you want to include variables in a sentence. Here are 3 ways to format strings:

1️⃣ Concatenation (not ideal):

name = "Alice" print("Hello " + name + "!") 
Enter fullscreen mode Exit fullscreen mode

2️⃣ str.format():

print("Hello, {}!".format(name)) 
Enter fullscreen mode Exit fullscreen mode

3️⃣ f-Strings (Best Practice in Python 3.6+):

print(f"Hello, {name}!") 
Enter fullscreen mode Exit fullscreen mode

f-strings are readable, fast, and the most modern way to format strings.

You can even do expressions inside them:

age = 25 print(f"In 5 years, you’ll be {age + 5} years old.") 
Enter fullscreen mode Exit fullscreen mode

📌 Bonus: Multiline Strings

Use triple quotes for multiline strings:

message = """Hello, This is a multi-line string in Python.""" print(message) 
Enter fullscreen mode Exit fullscreen mode

🚀 Recap

Today you learned:

  • How to define, access, and slice strings
  • How to join and repeat strings
  • Common string methods
  • Best practices for string formatting using f-strings

Top comments (0)