Sitemap
Mayuresh shewale

Mayuresh shewale

Data Analyst student

Python String Operations

There are 4 operations of string

2 min readFeb 12, 2022

--

String :- string is a sequence of characters that is enclosed in the quotations marks.

Press enter or click to view image in full size

{1} concatenation : Concatenation is the technique of combining or adding two strings.

Using + Operator

Example : # Concatenation two strings

name = “sky” + “blue”

print(name)

sky blue

{2} Replication : The replication operator produces the n number of copies of a string, n may be any integer.

Using * operator

Example : # Replication

print(“Red”*3)

print(3*”hi”)

print(7*3)

Output

RedRedRed

HiHiHi

Get Mayuresh shewale’s stories in your inbox

Join Medium for free to get updates from this writer.

777

{3} Indexing : Since all sequences are ordered and indexed of objects, each object stored in a sequence has it’s associated index number — positive one, zero indexed and starting from left, and the negative one starting at -1 from the right.

Press enter or click to view image in full size
Index

Example : # Indexing

[1] :- x="ABCD"

x[0]
'A'
[2] :- y=[0, 1, 2]

y[-1]
2
[3] :- a=("ABC", "DEF", "GHI")

a[1]
'DEF'

{4} Slicing : list slicing is a common practice and it is the most used technique for programmers to solve efficient problems. Consider a python list, In-order to access a range of elements in a list, you need to slice a list. One way to do this by i.e. colon(:)

Press enter or click to view image in full size
Slicing

Example : # Slicing

(1) :- a=[12,3,56,76,8,6,4,6]

a[1:4]

output

[3,56,76]

(2) :- b=[1,2,3,4,5,6,7,8,9]

b[-1: -2]

b[ : : 2]

b[ : : ]

Output:

Sliced b:
[9]
[1, 3, 5, 7, 9]
[1, 2, 3, 4, 5, 6, 7, 8, 9]

--

--