Working with Strings in Python 3
Last Updated :
07 Apr, 2025
In Python, sequences of characters are referred to as Strings. It used in Python to record text information, such as names. Python strings are "immutable" which means they cannot be changed after they are created.
Creating a String
Strings can be created using single quotes, double quotes, or even triple quotes. Python treats single quotes the same as double-quotes.
Python
# creating string
# with single Quotes
String = 'Hello Geek'
print("Creating string with single quotes :", String)
# Creating String
# with double Quotes
String = "yes, I am Geek"
print("Creating String with double quotes :", String)
# Creating String
# with triple Quotes
String = '''yes, I am Geek'''
print("Creating String with triple quotes :", String)
OutputCreating string with single quotes : Hello Geek
Creating String with double quotes : yes, I am Geek
Creating String with triple quotes : yes, I am Geek
Note: Be careful with quotes!
Python
# creating string
# with single quotes
String = 'Yes' I am geek'
print(String)
Output
File "<ipython-input-10-794636cfedda>", line 3
String = 'Yes' I am geek'
^
SyntaxError: invalid syntax
The reason for the error above is the single quote in Yes' I stopped the string. If you want to print ‘WithQuotes’ in python, this can’t be done with only single (or double) quotes alone, it requires simultaneous use of both. The best way to avoid this error use double-quotes.
Example:
Python
# this code prints the output within quotes.
# print WithQuotes within single quotes
print("'WithQuotes'")
print("Hello 'Python'")
# print WithQuotes within single quotes
print('"WithQuotes"')
print('Hello "Python"')
Output'WithQuotes'
Hello 'Python'
"WithQuotes"
Hello "Python"
Note: For more information, refer Single and Double Quotes | Python
String Indexing
Strings are a sequence of characters, which means Python can use indexes to call parts of the sequence. There are two ways of indexing.
- Positive Indexing
- Negative Indexing

Positive indexing
Python
# creating a string
String = "GEEK"
# Show first element in string
print("The 1st element is : ", String[0])
# Show 2nd element in string
print("The 2nd element is : ", String[1])
print("The 3rd element is : ", String[2])
print("The 4th element is : ", String[3])
OutputThe 1st element is : G
The 2nd element is : E
The 3rd element is : E
The 4th element is : K
Negative indexing
Python
# creating a string
String = "GEEK"
# Show last element in string
print("The 4th element is : ", String[-1])
# Show all element in string
print("The 3rd element is : ", String[-2])
print("The 2nd element is : ", String[-3])
print("The 1th element is : ", String[-4])
OutputThe 4th element is : K
The 3rd element is : E
The 2nd element is : E
The 1th element is : G
Updating Strings
In Python, Updation or deletion of characters from a string is not allowed. This will cause an error because item assignment or item deletion from a String is not supported. Python can allow you to reassign a new string to an existing one string.
Python
# Creating string
String = "Geeks"
# assign new character
String[0] = "Hi!, Geeks"
Output
Traceback (most recent call last): File "/home/b298782a4e04df4950426bf4bd5bee99.py", line 5, in <module> String[0] = "Hi!, Geeks" TypeError: 'str' object does not support item assignment
Updating the entire String
Python
# Creating string
String = "Hello Geeks"
print("Before updating : ", String)
# updating entire string
String = "Geeksforgeeks"
print("After updating : ", String)
# Update with indexing
String = 'Hello World!'
print("Updated String :- ", String[:6] + 'Python')
OutputBefore updating : Hello Geeks
After updating : Geeksforgeeks
Updated String :- Hello Python
String Slicing
Python slicing is about obtaining a sub-string from the given string by slicing it respectively from start to end.
Python slicing can be done in two ways.
- slice() Constructor
- Extending Indexing
Python
# Creating a String
String = "Geekforgeeks"
s1 = slice(3)
# print everything except the first element
print(String[s1])
# print everything UP TO the 6th index
print(String[:6])
# print everything between both index
print(String[1:7])
Slicing with negative Index.
Python
# Creating a String
String = "Geekforgeeks"
s1 = slice(-1)
# print everything except the last element
print(String[s1])
# print everything between both index
print(String[0:-3])
OutputGeekforgeek
Geekforge
We can use [ : : ] for specifying the frequency to print elements. It specifies the step after which every element will be printed starting from the given index. If nothing is given then it starts from the 0th index.
Python
# Creating a String
String = "Geekforgeeks"
# print everything with step 1
print(String[::1])
# print everything with step 2
print(String[2::2])
# print a string backwards
print(String[::-1])
OutputGeekforgeeks
efrek
skeegrofkeeG
Note: For more information, refer String Slicing in Python
String Formatting
str.format() and f-strings methods are used to add formatted objects to printed string statements. The string format() method formats the given string. It allows for multiple substitutions and value formatting.
Python
# using format option in a simple string
String = 'Geeksforgeeks'
print("{}, A computer science portal for geeks."
.format(String))
String = 'Geeks'
print("Hello {}, How are you ?".format(String))
# formatting a string using a numeric constant
val = 2
print("I want {} Burgers! ".format(val))
OutputGeeksforgeeks, A computer science portal for geeks.
Hello Geeks, How are you ?
I want 2 Burgers!
Note: For more information, refer Python | format() function
Formatted f-string literals are prefixed with 'f' and curly braces { } containing expressions that will be replaced with their values.
Python
# Creating string
String = 'GeekForGeeks'
print(f"{String}: A Computer Science portal for geeks")
# Creating string
String = 'Geek'
print(f"Yes, I am {String}")
# Manuplating int within {}
bags = 3
book_in_bag = 12
print(f'There are total {bags * book_in_bag} books')
# work with dictionaries in f-strings
Dic = {'Portal': 'Geeksforgeeks', 'for': 'Geeks'}
print(f"{Dic['Portal']} is a computer science portal for {Dic['for']}")
OutputGeekForGeeks: A Computer Science portal for geeks
Yes, I am Geek
There are total 36 books
Geeksforgeeks is a computer science portal for Geeks
Note: For more information, refer f-strings in Python 3 – Formatted string literals
Similar Reads
f-strings in Python Python offers a powerful feature called f-strings (formatted string literals) to simplify string formatting and interpolation. f-strings is introduced in Python 3.6 it provides a concise and intuitive way to embed expressions and variables directly into strings. The idea behind f-strings is to make
5 min read
string.whitespace in Python In Python, string.whitespace is a string containing all the characters that are considered whitespace. Whitespace characters include spaces, tabs, newlines and other characters that create space in text. string.whitespace constant is part of the string module, which provides a collection of string o
3 min read
String Translate using Dict - Python In Python, translating a string based on a dictionary involves replacing characters or substrings in the string with corresponding values from a dictionary. For example, if we have a string "python" and a dictionary {"p": "1", "y": "2"}, we can translate the string to "12thon". Letâs explore a few m
3 min read
Convert String to Set in Python There are multiple ways of converting a String to a Set in python, here are some of the methods.Using set()The easiest way of converting a string to a set is by using the set() function.Example 1 : Pythons = "Geeks" print(type(s)) print(s) # Convert String to Set set_s = set(s) print(type(set_s)) pr
1 min read
Why are Python Strings Immutable? Strings in Python are "immutable" which means they can not be changed after they are created. Some other immutable data types are integers, float, boolean, etc. The immutability of Python string is very useful as it helps in hashing, performance optimization, safety, ease of use, etc. The article wi
5 min read
Python - Check if substring present in string The task is to check if a specific substring is present within a larger string. Python offers several methods to perform this check, from simple string methods to more advanced techniques. In this article, we'll explore these different methods to efficiently perform this check.Using in operatorThis
2 min read
Convert string to a list in Python Our task is to Convert string to a list in Python. Whether we need to break a string into characters or words, there are multiple efficient methods to achieve this. In this article, we'll explore these conversion techniques with simple examples. The most common way to convert a string into a list is
2 min read
Check if String Contains Substring in Python This article will cover how to check if a Python string contains another string or a substring in Python. Given two strings, check whether a substring is in the given string. Input: Substring = "geeks" String="geeks for geeks"Output: yesInput: Substring = "geek" String="geeks for geeks"Output: yesEx
8 min read
Convert integer to string in Python In this article, weâll explore different methods for converting an integer to a string in Python. The most straightforward approach is using the str() function.Using str() Functionstr() function is the simplest and most commonly used method to convert an integer to a string.Pythonn = 42 s = str(n) p
2 min read
Python String Formatting - How to format String? String formatting allows you to create dynamic strings by combining variables and values. In this article, we will discuss about 5 ways to format a string.You will learn different methods of string formatting with examples for better understanding. Let's look at them now!How to Format Strings in Pyt
9 min read