DEV Community

Lokesh
Lokesh

Posted on

Strings in Python

Introduction

Strings are used to store text, such as names, words, sentences, and special characters.

What Are Strings in Python?

A string is a sequence of characters enclosed within single quotes, double quotes, or triple quotes.

Strings are commonly used to store text data in Python.

Example:

name = "Python"
city = 'Chennai'
message = """Welcome to Python Programming"""

print(name)
print(city)
print(message)
Enter fullscreen mode Exit fullscreen mode

Output:

Python
Chennai
Welcome to Python Programming
Enter fullscreen mode Exit fullscreen mode

How to Create a String in Python?

Python provides three ways to create strings.

1. Single Quotes

We can create a string using single quotes.

name = 'Arun'
print(name)
Enter fullscreen mode Exit fullscreen mode

2. Double Quotes

We can also create a string using double quotes.

name = "Python"
print(name)
Enter fullscreen mode Exit fullscreen mode

3. Triple Quotes

Triple quotes are used to create multiline strings.

message = """Hello,
Welcome to Python.
Let's start learning!"""

print(message)
Enter fullscreen mode Exit fullscreen mode

String Indexing in Python

Indexing is used to access individual characters from a string.

In Python, indexing starts from 0.

There are two types of indexing:

  • Positive Indexing: Starts from the beginning of the string (0, 1, 2, 3...).
  • Negative Indexing: Starts from the end of the string (-1, -2, -3...).

Example:

name = "Python"

print(name[0])
print(name[1])
print(name[-1])
print(name[-2])
Enter fullscreen mode Exit fullscreen mode

Output:

P
y
n
o
Enter fullscreen mode Exit fullscreen mode

String Slicing in Python

String slicing is used to extract a part of a string.

The syntax is:

string[start:end:step]
Enter fullscreen mode Exit fullscreen mode
  • start: Starting index.
  • end: Ending index (not included).
  • step: Number of positions to move each time.

Example:

name = "Python Programming"

print(name[0:6])
print(name[7:18])
print(name[:6])
print(name[7:])
Enter fullscreen mode Exit fullscreen mode

Output:

Python
Programming
Python
Programming
Enter fullscreen mode Exit fullscreen mode

String Operations in Python

Python supports different operations on strings.

1. Concatenation

Concatenation is used to combine two or more strings using the + operator.

Example:

first = "Hello"
second = "Python"

result = first + " " + second

print(result)
Enter fullscreen mode Exit fullscreen mode

Output:

Hello Python
Enter fullscreen mode Exit fullscreen mode

2. Repetition

The * operator is used to repeat a string multiple times.

Example:

word = "Python "

print(word * 3)
Enter fullscreen mode Exit fullscreen mode

Output:

Python Python Python
Enter fullscreen mode Exit fullscreen mode

3. Membership

Membership operators are used to check whether a particular character or word exists in a string.

The operators are:

  • in
  • not in

Example:

text = "Python Programming"

print("Python" in text)
print("Java" not in text)
Enter fullscreen mode Exit fullscreen mode

Output:

True
True
Enter fullscreen mode Exit fullscreen mode

Common String Methods in Python

Python provides several built-in string methods to perform different operations.

Method Description
upper() Converts the string into uppercase.
lower() Converts the string into lowercase.
capitalize() Converts the first character to uppercase.
title() Converts the first character of each word to uppercase.
strip() Removes leading and trailing whitespace.
replace() Replaces a specified part of a string.
split() Splits a string into a list.
find() Returns the index of the first occurrence of a substring.
count() Counts the occurrences of a character or substring.

Example:

text = "hello python"

print(text.upper())
print(text.lower())
print(text.capitalize())
print(text.title())
print(text.replace("python", "world"))
Enter fullscreen mode Exit fullscreen mode

Output:

HELLO PYTHON
hello python
Hello python
Hello Python
hello world
Enter fullscreen mode Exit fullscreen mode

String Immutability in Python

Strings in Python are immutable. This means we cannot directly change individual characters of an existing string.

Example:

name = "Python"

name[0] = "J"

print(name)
Enter fullscreen mode Exit fullscreen mode

Output:

TypeError
Enter fullscreen mode Exit fullscreen mode

If we want to change a string, we need to create a new string.

Correct Example:

name = "Python"
new_name = "J" + name[1:]

print(new_name)
Enter fullscreen mode Exit fullscreen mode

Output:

Jython
Enter fullscreen mode Exit fullscreen mode

Why Are Strings Important in Python?

Strings are important because:

  1. They help us store and manage text data.
  2. They are useful for handling names, addresses, and messages.
  3. They help process and manipulate text.
  4. They are used in web development and data processing.
  5. They make it easier to work with user input.

Top comments (0)