Python Strings

 Python Strings:

Strings are sequences of characters strings.


Example 1:

‘Python’ and “HRS INTERSTING FACTS”

You can also use multi-line strings by using triple quotes (“”” or’’’)

Triple quotes we already discuss about Triple quotes. 


Example 2:

“”” Hello guys,

Welcome to HRS INTERSTING FACTS

Python Programming Tutorial.”””

Example Program:

st1='hrs'

st2="interesting"

st3=""" Hello guys,

Welcome to HRS INTERSTING FACTS

Python Programming Tutorial."""

st4="let's Start"

print(st1)

print(st2)

print(st3)

print(st4)

Output:

hrs

interesting

 Hello guys,

Welcome to HRS INTERSTING FACTS

Python Programming Tutorial.

let's Start

In above example st4="let's Start" is valid.

If you type st4=’let's Start’ is invalid. Because single quote started and ended again single quote start but not end so, by using double quotes can solve it.

We already learn what is strings and example. Now we will learn about sequence operations in strings. Let’s start.

Sequence Operations in strings

Concatenation: Adding two or more stings by using + operator

‘python’+’tutorial’ à python tutorial’

Repetition: Using * operator, it repeats strings in given number times.

Strings follows * and specify the how many times repeat the given strings.

Example:

 ‘python’*2= python python

Slicing: using : colon operation, we can access the range of item in a strings. 

st=’python’àst[2:6]à thon

Indexing: st=’pythonàst[3]à h

len (): It used to find string length.

st=’python’à len(st)à6

Let’s See Program:

# sequences operations in strings

 

st1 = 'hrs'

st2 = 'interesting'

 

# concatenation

print(st1 + st2)

 

# repetition

print(st1 * 2)

 

# slicing

print(st1[0:1])

print("negative slcing=",st2[-3])

 

#slicing 6th to 2nd last character

print("str2[5:-2] =",st2[5:-2])

 

# index

print("Index=",st2[4])

print("interesting st2[-1]",st2[-1])

 

#len()

print(len(st2))

Output:

hrsinteresting

hrshrs

h

negative slcing= i

str2[5:-2] = esti

Index= r

interesting st2[-1] g

11

Above Program Explanation:

I hope you guys, you understand concatenation and Repetition.

So, let’s move on slicing, index and length  

In above example program,

# slicing

print(st1[0:1])

print("negative slcing=",st2[-3

Explanation 1: First st1 = 'hrs' and st2 = 'interesting',

st1 have 3 element “hrs” it starts with 0 to 2 indexes. So, it start with ‘h’ and at end ‘r’. but ended index is not consider by using range (). Final output is h

Explanation 2:  st2 have 11 elements, ‘interesting’. St2[-3] means it takes in the reverse order.

Output is i (g=-1,n=-2,i=-3 up to i=-11).

I hope you guys, you will understand it. If you have any quires, please contact me or leave a comment.

# index

print("Index=",st2[4])

print("interesting st2[-1]",st2[-1])

Explanation 3: I hope Positive index value, you may know it. Let’s see negative index.

St2=”interesting”

Negative index is taking in the reverse order,

Output is g (g=-1 index value so it prints g)

Explanation 4:

#len()

print(len(st2))

st2=”interesting”, it have 11 element . So, it prints 11.

Output is 11(length of the string is 11)


Could you create a program by using sequence of strings concepts

operations?

If you create a own program, send your program code to contact form. 

If you have any quires/doubt , please leave a comment or contact me in the contact form.

I hope you guys, was this article helpful you? Please share your feedback