Skip to main content
  1. Notes/
  2. Python 🐍/

🎬 Strings in Python | Lesson 04

Python Progmramming Course
Table of Contents
Python - This article is part of a series.
Part 4: This Article

🎬 Strings in Python
#

Strings are the heart of working with text in Python. Whether you want to print a greeting, store a user’s name, or process sentences, strings make it possible! Let’s explore how strings work and the magic you can do with them. ✨


What is a String? 🧩
#

A string is simply textβ€”that can be letters, numbers, symbolsβ€”inside quotes.

greeting = "Hello, World!"
  • You can use single (’ ‘) or double (" “) quotes.

Creating Strings ✍️
#

name = "Amit"
sentence = 'Python is fun!'

String Operations 🎲
#

1. Concatenation (Joining Strings)
#

You can join (concatenate) strings using the + operator.

first = "Good"
second = "Morning"
message = first + " " + second
print(message)   # Good Morning

2. Repetition
#

Use * to repeat a string.

echo = "Hi! " * 3
print(echo)   # Hi! Hi! Hi!

String Indexing & Slicing πŸ”ͺ
#

You can access individual characters or parts (slices) of a string.

text = "Python"
print(text[0])     # 'P' (first character)
print(text[-1])    # 'n' (last character)
print(text[1:4])   # 'yth' (characters 1 to 3)

Useful String Methods πŸ› οΈ
#

  • .upper() – Convert to uppercase
  • .lower() – Convert to lowercase
  • .title() – Capitalize each word
  • .strip() – Remove spaces from start/end
  • .replace(old, new) – Replace old with new text
  • .find(substring) – Find position of substring
greet = " hello there! "
print(greet.strip())           # "hello there!"
print(greet.upper())           # " HELLO THERE! "
print("apple".replace('a', 'A'))   # "Apple"
print("Python".find('th'))     # 2

Formatted Strings (f-strings) πŸͺ„
#

Make strings with variables easy to read using f-strings.

name = "Sara"
age = 20
print(f"My name is {name} and I am {age} years old.")
# Output: My name is Sara and I am 20 years old.

Practice Questions with Solutions πŸ†
#

  1. Practice: Join and print “Good” and “Evening”.
    • Solution:
print("Good" + " " + "Evening")
  1. Practice: How do you print the first and last letter of “Python”?
    • Solution:
text = "Python"
print(text, text[-1])  # P n
  1. Practice: Change “python” to “PYTHON”.
    • Solution:
print("python".upper())

Step-by-Step Mini Project: Greeting Builder πŸŽ‰
#

Task: Ask the user for their name and create a personalized greeting such as Hello, Priya! Welcome to Python.

Solution:

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

Checklist for This Chapter βœ…
#

  • Created and joined strings
  • Indexing and slicing text
  • Used useful string methods
  • Built f-strings for formatted output

Awesome! You’re now skilled at handling text with Python strings. Try out message creators, name extractors, or emoji repeaters! πŸ₯³

Aryan
Author
Aryan
A little bit about you
Python - This article is part of a series.
Part 4: This Article

Related

⏰ Working with Date and Time in Python | Lesson 16
Python Progmramming Course
πŸ—οΈ Object-Oriented Programming (OOP) Concepts in Python | Lesson 15
Python Progmramming Course
πŸ‘Ά Python Basics | Lesson 02
Python Progmramming Course
πŸ“„ File Handling in Python | Lesson 14
Python Progmramming Course
πŸ“‹ Lists in Python | Lesson 05
Python Progmramming Course
πŸ“– Dictionaries in Python | Lesson 07
Python Progmramming Course