← Back to Python

All Topics

Advertisement

Learn/Python/Python Fundamentals

Strings

Topic: Data Types

Advertisement

Introduction

Strings are sequences of characters used to store text data.

String Operations

text = "Hello, Python!"

# Indexing and slicing
print(text[0])      # H
print(text[0:5])    # Hello
print(text[-6:])    # Python!

# String methods
print(text.upper())      # HELLO, PYTHON!
print(text.lower())      # hello, python!
print(text.replace("Python", "World"))
print(text.split(","))   # ['Hello', ' Python!']

String Formatting

name = "Alice"
age = 30

# f-strings (Python 3.6+)
print(f"My name is {name} and I'm {age}")

# format method
print("My name is {} and I'm {}".format(name, age))

# old style
print("My name is %s and I'm %d" % (name, age))

Practice Problems

  1. Count vowels and consonants in a string
  2. Check if string is palindrome
  3. Remove duplicates from string
  4. Find all occurrences of a substring
  5. Encrypt string using Caesar cipher

Advertisement

Advertisement

Need More Practice?

Get personalized Python help from ChatWhole's AI-powered platform.

Get Expert Help →