Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is String in python?
2.1.
Syntax for Strings in Python
2.2.
Examples
3.
Creating Strings in Python
3.1.
Single Line Strings
3.2.
Python
3.3.
Multi-Line Strings
3.4.
Python
4.
Accessing characters of the String
4.1.
Accessing Single Character
4.2.
Python
4.3.
Accessing Multiple Characters: Slicing
4.4.
Python
5.
String Length
5.1.
Python
6.
Looping Through a String
6.1.
Python
7.
Deletion in a String
7.1.
Deleting a character
7.2.
Python
7.3.
Deleting an entire string:
7.4.
Python
8.
Updation in a String
8.1.
Updating a character in the string.
8.2.
Python
8.3.
Updating an entire string:
8.4.
Python
9.
Repeat a String
9.1.
Python
10.
Formatting of Strings
10.1.
Python
11.
Frequently Asked Questions
11.1.
What is string data in Python?
11.2.
Why use strings in Python?
11.3.
Why is it called string?
12.
Conclusion
Last Updated: Jul 13, 2024
Easy

Strings in Python

Author GAZAL ARORA
0 upvote

Introduction

Strings are one of the most fundamental and versatile data types in Python. They are used to represent text and are essential for any programming task that involves communication between the user and the program. Whether you're manipulating text, parsing data, or simply printing output, understanding how to work with strings is crucial.

Strings in Python

Also see, Merge Sort Python

What is String in python?

In Python, a string is a sequence of characters used to represent text. Strings are immutable, meaning once created, their contents cannot be changed. You can create strings by enclosing characters in single quotes (' '), double quotes (" "), or triple quotes (''' ''' or """ """).

Syntax for Strings in Python

  1. Single Quotes: 'Hello, World!'
  2. Double Quotes: "Hello, World!"
  3. Triple Quotes: '''Hello, World!''' or """Hello, World!"""

Examples

Single Quotes

single_quote_string = 'Hello, World!'
print(single_quote_string)

 

Double Quotes

double_quote_string = "Hello, World!"
print(double_quote_string)

 

Triple Quotes

Triple quotes are particularly useful for multi-line strings and strings that contain both single and double quotes.

multi_line_string = '''Hello,
World!
This is a multi-line string.'''
print(multi_line_string)

Creating Strings in Python

Single Line Strings

A string in Python is surrounded by either single quotation marks or double quotation marks. E.g., 'Coding Ninjas' is the same as "Coding Ninjas."

  • Python

Python

# Creating and printing strings with single quotation marks.
string1 = 'Hello Ninjas'
print(string1)

# Creating strings with double quotation marks.
string2 = "Welcome to Coding Ninjas"
print(string2)

# Printing directly.
print('Hello Ninjas')
print("Welcome to Coding Ninjas")
You can also try this code with Online Python Compiler
Run Code

Output:

Multi-Line Strings

You can use three quotes to declare a multi-line String in Python.

  • Python

Python

# Creating and printing multi-line strings with triple quotation marks.



string1 = '''Hello Ninjas,
Welcome to Coding Ninjas'''


print(string1)
You can also try this code with Online Python Compiler
Run Code

Output:

Check this out: Fibonacci Series in Python

Accessing characters of the String

Accessing Single Character

Since strings are like arrays, individual characters can be accessed using Indexing. The index must be an integer, and it starts from 0. 

Python also supports negative Indexing. The index value of -1 denotes the last item, the index of -2 the second last item, and so on. 

Trying to access a character outside the index range will result in an IndexError.
 

  • Python

Python

string = "Coding Ninjas"
print("Original String: ")
print(string)

# Printing 1st character.
print("\nFirst character of the string is: ")
print(string[0])

# Printing the last character.
print("\nLast character of the string is: ")
print(string[-1])
You can also try this code with Online Python Compiler
Run Code

Output:

You can practice by yourself with the help of online python compiler for better understanding.

Accessing Multiple Characters: Slicing

The slicing method is used to access a range of characters in the String. A Slicing operator is used to slice a String (colon).

For example, 

String = “Coding Ninjas”

To print the characters odi of Coding Ninjas, 

we have to use: print(String[1:4]). 

  • Python

Python

string = "Coding Ninjas"
print("Original String: ")
print(string)

# Printing odi.
print("\nPrinting odi")
print(string[1:4])

# Printing characters between 2rd to 10th index.
print("\nPrinting characters between 2rd to 10th index:")
print(string[2:10])

# Printing characters between 3rd and the last character.
print("\nPrinting characters between 3rd and the last character:")
print(string[2:-1])
You can also try this code with Online Python Compiler
Run Code

Output:

String Length

Python provides the len() method to determine the length of a string.

  • Python

Python

string = "Coding Ninjas"
print("Original String: ")
print(string)

print("\nLength of the string 'Coding Ninjas' is: ")
print(len(string))
You can also try this code with Online Python Compiler
Run Code

Output:

Looping Through a String

Since strings are arrays, we can use a for loop to traverse through the characters in a string.

  • Python

Python

string = "Ninjas"
print("Original String: ")
print(string)

for x in string:
print(x)
You can also try this code with Online Python Compiler
Run Code

Output:

Deletion in a String

The deletion of characters from a string is not allowed in Python. This will result in an error since the deletion of an item from String are is not supported. However, the entire String can be deleted with the built-in del Keyword.

Deleting a character

For example, deleting a character with index 2 from string "Coding Ninjas".

  • Python

Python

string = "Coding Ninjas"
print("Original String: ")
print(string)

del(string[2])
print(string)
You can also try this code with Online Python Compiler
Run Code

Output

Deleting an entire string:

  • Python

Python

string = "Coding Ninjas"
print("Original String: ")
print(string)

del(string)
print(string)
You can also try this code with Online Python Compiler
Run Code

Output:

Updation in a String

The updation of characters from a string is not allowed in Python. This will result in an error since the updation of an item from String are is not supported. Since strings are immutable, elements of a String cannot be modified after being assigned. Only new strings can be reassigned to the same name.

Updating a character in the string.

For example, updating a character with index 2 to ‘a’.

  • Python

Python

string = "Coding Ninjas"
print("Original String: ")
print(string)

string[2] = 'a'
print(string)
You can also try this code with Online Python Compiler
Run Code

Output:

Updating an entire string:

  • Python

Python

string = "Coding Ninjas"
print("Original String: ")
print(string)

print("\nModified String: ")
string = 'Welcome Ninjas'
print(string)
You can also try this code with Online Python Compiler
Run Code

Output:

Repeat a String

Use the * symbol to repeat a string.

  • Python

Python

string = 'Coding'
string = string * 3
print(string)
You can also try this code with Online Python Compiler
Run Code

Output:

Also see, Python Filter Function

Formatting of Strings

Python also provides the format() function, a very versatile and powerful tool for formatting Strings. To set the order, the Format method in String uses curly braces {} as placeholders that can hold arguments based on position or keyword.

Syntax:

"Any sentence {variable1} some text {variable2}".format(variable1 = "DATA", variable1 = "DATA")

 

For example,

  • Python

Python

name = ["John", "Aarti", "Rahul", "Karan"]
age = [20, 22, 21, 25]

for i in range(4):
print("Hi, My name is {var1} and I am {var2} years old.".format( var1 = name[i], var2 = age[i]))
You can also try this code with Online Python Compiler
Run Code

Output:

Check out Escape Sequence in Python

Frequently Asked Questions

What is string data in Python?

String data in Python represents sequences of characters enclosed in quotes, used to handle and manipulate text in various ways.

Why use strings in Python?

Strings in Python are essential for text manipulation, data input/output, and communication between users and programs, making them vital for most applications.

Why is it called string?

It's called "string" because it represents a sequence of characters linked together, similar to how beads are strung on a thread.

Conclusion

In this article, we learned about Strings in Python. We also learned about 

  • Accessing the characters in the string.
  • Looping in a string.
  • Calculating the length of a string.
  • Deletion/Updation in the string.
  • Formatting in the string.

Click here to learn about String Manipulation in Python.
Check out this article - String slicing in Python

Want to learn Python but don't know where to start?

Start here. Coding Ninjas provide this amazing course on Python. If you are just getting into coding and want to build a strong foundation, this course is for you.

Recommended problems -

You can also consider our paid courses such as DSA in Python to give your career an edge over others!

Live masterclass