Strings | Dataplexa

Strings in Python

Strings are one of the most important data types in Python. A string represents a sequence of characters such as letters, numbers, symbols, or even spaces. Everything inside quotes (' ' or " ") is treated as a string.

You will use strings everywhere — printing messages, user input, file names, data cleaning, APIs, and more. Understanding strings is essential for becoming a strong Python programmer.

What Is a String?

A string is simply text enclosed within quotes. Python allows single, double, and even triple quotes.

name = "Sreekanth"
greeting = 'Hello from Dataplexa!'
multiline = """This is a 
multi-line string."""

Triple quotes allow the string to span multiple lines.

Why Strings Are Important

  • Used in printing, logging, and messaging
  • Used heavily in web development
  • Critical for data cleaning in data science
  • Essential for user inputs and outputs
  • Used in APIs, JSON, file names, machine learning pipelines, etc.

Accessing Characters in a String

You can access characters in a string using indexing. Indexing starts at 0.

word = "Dataplexa"

print(word[0])   # D
print(word[3])   # a
print(word[-1])  # last character (a)

Negative indexing starts from the end of the string.

Slicing Strings

Slicing allows you to extract a part of a string.

text = "Python Programming"

print(text[0:6])    # Python
print(text[7:18])   # Programming
print(text[:6])     # Python
print(text[7:])     # Programming

String Length

Use len() to find how long a string is.

msg = "Welcome"
print(len(msg))   # 7

Important String Methods

Python has many built-in methods to work with strings. Here are the most commonly used ones:

MethodDescription
upper()Converts all letters to uppercase
lower()Converts all letters to lowercase
title()Makes each word start with capital letter
strip()Removes spaces from both sides
replace(a, b)Replaces text
split()Splits a string into a list
join()Joins list elements into a string

Examples of String Methods

name = "  dataplexa academy  "

print(name.upper())      # DATAPLEXA ACADEMY
print(name.strip())      # dataplexa academy
print(name.replace("academy", "Python School"))
print(name.split())      # ['dataplexa', 'academy']

String Concatenation

You can combine strings using the + operator.

first = "Data"
second = "plexa"

full = first + second
print(full)  # Dataplexa

String Formatting

String formatting is the best way to insert variables into strings.

Using f-strings (recommended)

name = "Sreekanth"
age = 25

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

Using format()

print("My name is {} and I am {} years old".format(name, age))

Checking Substrings

You can check if certain text exists inside a string.

msg = "Welcome to Dataplexa"

print("Data" in msg)     # True
print("Python" in msg)   # False

Iterating Through Strings

Strings are sequences, so you can loop through them.

for ch in "Python":
    print(ch)

Real-World Example: Cleaning User Input

This example shows how to clean and format names collected from a form.

name = input("Enter your name: ")

cleaned = name.strip().title()

print("Clean Name:", cleaned)

Real-World Example: Counting Characters

text = "Python programming is powerful"
count = text.count("p")

print("Number of 'p' characters:", count)

📝 Practice Exercises


  1. Create a string and print its first and last characters.
  2. Write a program to count vowels in a string.
  3. Ask the user for a sentence and print it in reverse.
  4. Write a program that replaces all spaces with hyphens.
  5. Check whether the entered string is a palindrome.

✅ Practice Answers


Answer 1:

s = "Dataplexa"
print(s[0], s[-1])

Answer 2:

text = input("Enter text: ").lower()
vowels = 0

for ch in text:
    if ch in "aeiou":
        vowels += 1

print("Vowel count:", vowels)

Answer 3:

sentence = input("Enter a sentence: ")
print(sentence[::-1])

Answer 4:

s = input("Enter text: ")
print(s.replace(" ", "-"))

Answer 5:

text = input("Enter text: ")
if text == text[::-1]:
    print("Palindrome")
else:
    print("Not a palindrome")