Skip to main content

Python Strings: Definition, Operations, and Examples

 

 ✍️Introduction

In Python, a string is used to store text data.

Strings are one of the most important data types and are used in almost every Python program.

In this article, you will learn:

  • What strings are

  • How to create strings

  • String operations

  • Common string methods with examples


What is a String in Python?

A string is a sequence of characters enclosed inside:

  • Single quotes ' '

  • Double quotes " "

Example:

name = "Python"


Creating Strings in Python

str1 = "Hello"
str2 = 'World'

Both are valid in Python.


Accessing Characters in a String

Each character has an index number, starting from 0.

text = "Python" print(text[0]) # P print(text[3]) # h

String Length

Use len() to find string length.


text = "Python" print(len(text)) # 6

String Concatenation

Joining two strings using +.

a = "Hello" b = "Python" print(a + " " + b)

String Repetition

Repeat string using *.


print("Hi " * 3)

String Slicing

Extract part of a string.

text = "Programming" print(text[0:6]) # Progra

String Methods (Very Important)

1️⃣ lower() and upper()

text = "Python" print(text.lower()) print(text.upper())


2️⃣ strip()

Removes spaces.

text = " Python " print(text.strip())

3️⃣ replace()

text = "I like Java" print(text.replace("Java", "Python"))

4️⃣ find()

text = "Python Programming" print(text.find("Python"))

5️⃣ split()

text = "Learn Python Easily" print(text.split())

Checking String Content

text = "Python123" print(text.isalpha()) print(text.isdigit()) print(text.isalnum())

String Comparison

a = "python" b = "Python" print(a == b)

❌ Strings are Immutable

Strings cannot be changed after creation.

Wrong:

text[0] = 'J' # Error

Correct:

text = "Java"


🎯 Why Strings are Important?

  • Used in input/output

  • Used in data processing

  • Important for exams & interviews

  • Used in real-life applications


Real-Life Example

email = "student@gmail.com" if "@" in email: print("Valid email")

Conclusion

Strings are one of the most commonly used data types in Python.
Understanding string operations and methods will help you write better programs.

Practice string examples daily to master Python.


💬Let’s Interact!

What will be the output?

text = "Python" print(text[1:4])

Comment your answer 👇😊


📌 Related Articles:

Comments

popular

What Is Python Programming? Features, Uses and Career Scope.

  ✍️ Introduction Python is one of the most popular and beginner-friendly programming languages in the world today. It is widely used for web development, data science, artificial intelligence, automation, and software development. Because of its simple syntax and powerful features, Python is an excellent choice for students, beginners, and professionals. In this article, you will learn what Python programming is, its key features, real-life uses, and the career opportunities it offers. 🧠 What Is Python Programming? Python is a high-level, interpreted programming language created to make coding easy and readable. It allows developers to write programs using simple English-like statements, which makes it ideal for beginners. Python supports multiple programming styles such as procedural, object-oriented, and functional programming. ⭐ Features of Python Easy to learn and understand Simple and readable syntax Interpreted language Platform independent Large standard l...

what is Python Date and Time ? Complete Guide with Examples:

  ✍️ Introduction Python provides powerful tools to work with dates and times . The most commonly used module is datetime . The datetime module allows Python programs to create, manipulate, format, and perform calculations with dates and times easily. Date and time handling is used in: Logging systems Attendance & billing systems Data analysis Real-time applications How To Import Date and Time Module : In Python, the datetime module is used to work with dates , times , and date–time combinations . It is part of Python’s standard library, so no installation is required. You can import it in different ways depending on your need. import datetime Get Current Date and Time: This code obtains the current system date and time as a single datetime object using the datetime module. Example: import datetime now = datetime.datetime.now() print (now) Output: 2026-02-13 11:25:53.445827 "Code executed successfully"  Get Only Date: This code retrieves t...

Features of Python Programming Language Explained with Examples

✍️ Introduction Python is one of the most popular programming languages because of its powerful and easy-to-use features. It is designed to make programming simple, readable, and efficient. Python is widely used by beginners as well as professionals due to its flexibility and strong community support. In this article, we will discuss the main features of Python programming language in simple English with easy examples. 🧠 What Are Features of Python? Features of Python are the special characteristics that make it different from other programming languages. These features help programmers write clean, readable, and efficient code with less effort. ⭐ Key Features of Python Programming Language 1️⃣ Easy to Learn and Easy to Use Python has a simple syntax that is close to the English language. This makes it very easy for beginners to learn and understand programming concepts. Example: print("Hello, World!") 2️⃣ Interpreted Language Python is an interpreted language, which me...

Python Operators: Types with Examples

 ✍️Introduction In Python, operators are symbols used to perform operations on variables and values. Operators help us do calculations, comparisons, and logical decisions in programs. In this article, you will learn: • What Python operators are • Types of Python operators • Simple examples for each type