0% found this document useful (0 votes)
28 views9 pages

Mca Unit4

Uploaded by

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

Mca Unit4

Uploaded by

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

Python String

Python string is the collection of the characters


surrounded by single quotes, double quotes, or
triple quotes. The computer does not
understand the characters; internally, it stores
manipulated character as the combination of
the 0's and 1's.
Each character is encoded in the ASCII or
Unicode character. So we can say that Python
strings are also called the collection of Unicode
characters.
In Python, strings can be created by enclosing
the character or the sequence of characters in
the quotes. Python allows us to use single
quotes, double quotes, or triple quotes to
create the string.
Creating String in Python
We can create a string by enclosing the
characters in single-quotes or double- quotes.
Python also provides triple-quotes to represent
the string, but it is generally used for multiline
string or docstrings.
1.#Using single quotes
2.str1 = 'Hello Python'
3.print(str1)
4.#Using double quotes
5.str2 = "Hello Python"
6.print(str2)
7.
8.#Using triple quotes
9.str3 = '''''Triple quotes are generally used for
10. represent the multiline or
11. docstring'''
12. print(str3)
Output:
Hello Python
Hello Python
Triple quotes are generally used
for
represent the multiline or
docstring
Strings indexing and splitting
Like other languages, the indexing of the
Python strings starts from 0. For example, The
string "HELLO" is indexed as given in the below
figure.

Consider the following example:


1.str = "HELLO"
2.print(str[0])
3.print(str[1])
4.print(str[2])
5.print(str[3])
6.print(str[4])
7.# It returns the IndexError because 6th index do
esn't exist
8.print(str[6])
Output:
H
E
L
L
O
IndexError: string index out of
range

As shown in Python, the slice operator [] is used


to access the individual characters of the string.
However, we can use the : (colon) operator in
Python to access the substring from the given
string. Consider the following example.
Here, we must notice that the upper range
given in the slice operator is always exclusive
i.e., if str = 'HELLO' is given, then str[1:3] will
always include str[1] = 'E', str[2] = 'L' and
nothing else.
Consider the following example:
1.# Given String
2.str = "JAVATPOINT"
3.# Start Oth index to end
4.print(str[0:])
5.# Starts 1th index to 4th index
6.print(str[1:5])
7.# Starts 2nd index to 3rd index
8.print(str[2:4])
9.# Starts 0th to 2nd index
10. print(str[:3])
11. #Starts 4th to 6th index
12. print(str[4:7])
Output:
JAVATPOINT
AVAT
VA
JAV
TPO

We can do the negative slicing in the string; it


starts from the rightmost character, which is
indicated as -1. The second rightmost index
indicates -2, and so on. Consider the following
image.

Consider the following example


1.str = 'JAVATPOINT'
2.print(str[-1])
3.print(str[-3])
4.print(str[-2:])
5.print(str[-4:-1])
6.print(str[-7:-2])
7.# Reversing the given string
8.print(str[::-1])
9.print(str[-12])
Output:
T
I
NT
OIN
ATPOI
TNIOPTAVAJ
IndexError: string index out of
range

The format() method


The format() method is the most flexible and
useful method in formatting strings. The curly
braces {} are used as the placeholder in the
string and replaced by the format() method
argument. Let's have a look at the given an
example:
1.# Using Curly braces
2.print("{} and {} both are the best friend".format(
"Devansh","Abhishek"))
3.
4.#Positional Argument
5.print("{1} and {0} best players ".format("Virat","R
ohit"))
6.
7.#Keyword Argument
8.print("{a},{b},{c}".format(a = "James", b = "Peter
", c = "Ricky"))
Output:
Devansh and Abhishek both are the
best friend
Rohit and Virat best players
James,Peter,Ricky

You might also like