0% found this document useful (0 votes)
4 views10 pages

Python Strings

The document explains strings in Python as immutable data structures that represent sequences of characters. It covers how to create strings using single, double, and triple quotes, as well as accessing characters through indexing and slicing. Additionally, it highlights that updating or deleting characters directly in a string is not allowed, but the entire string can be deleted using the 'del' keyword.

Uploaded by

22bcais046
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views10 pages

Python Strings

The document explains strings in Python as immutable data structures that represent sequences of characters. It covers how to create strings using single, double, and triple quotes, as well as accessing characters through indexing and slicing. Additionally, it highlights that updating or deleting characters directly in a string is not allowed, but the entire string can be deleted using the 'del' keyword.

Uploaded by

22bcais046
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

PYTHON

STRINGS
R.K.SUSHMITA LAKSHME
WHAT IS STRING
A String is a data structure in Python that represents a
sequence of characters.
It is an immutable data type
Strings are used widely in many different applications,
such as storing and manipulating text data,
representing names, addresses, and other types of
data that can be represented as text.
CREATING A STRING IN PYTHON
Strings in Python can be created using single quotes or double quotes or even triple quotes.
Let us see how we can define a string in Python.

String1 = 'Welcome to the Geeks World'


print("String with the use of Single Quotes: ")
print(String1)
String with the use of Single Quotes:
Welcome to the Geeks World
String1 = "I'm a Geek" String with the use of Double Quotes:
print("\nString with the use of Double Quotes: ")
print(String1)
I'm a Geek
String with the use of Triple Quotes:
I'm a Geek and I live in a world of "Geeks"
String1 = "I'm a Geek"
print("\nString with the use of Double Quotes: ")
print(String1)
In Python, individual
characters of a String can
be accessed by using the
method of Indexing.
Indexing allows negative
address references

While accessing an index out of


the range will cause an
IndexError. Only Integers are
allowed to be passed as an index,
float or other types that will
cause a TypeError.

ACCESSING CHARACTERS IN
PYTHON STRING
STRING SLICING
In Python, the String Slicing method is used to access a range of characters in the
String. Slicing in a String is done by using a Slicing operator, i.e., a colon (:).
REVERSING A PYTHON
STRING
By accessing characters from a string, we can also reverse strings in Python. We can Reverse a
string by using String slicing method.

Example:
In this example, we will reverse a string by accessing the index. We did not specify the first
two parts of the slice indicating that we are considering the whole string, from the start index
to the last index.

gfg = "geeksforgeeks" skeegrofskeeg


print(gfg[::-1])
DELETING/UPDATING
FROM A STRING
In Python, the 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.
deletion of the entire String is possible with the use of a
built-in del keyword.
UPDATE EX: DELETE EX:
String1 = "Hello, I'm a Geek" String1 = "Hello, I'm a Geek"
print("Initial String: ") print("Initial String: ")
print(String1) print(String1)
list1 = list(String1)
list1[2] = 'p' del String1
String2 = ''.join(list1) print("\nDeleting entire String: ")
print("\nUpdating character at 2nd Index: ") print(String1
print(String2)

Initial String:
NameError: name 'String1' is not defined
Hello, I'm a Geek
Updating character at 2nd Index:
Heplo, I'm a Geek

02 - WEBSITE
THANK YOU

You might also like