0% found this document useful (0 votes)
9 views3 pages

Class7 Datatype String

The document provides examples of string concatenation, indexing, and slicing in Python. It demonstrates various operations such as combining strings, accessing characters using positive and negative indexing, and extracting substrings through slicing. Additionally, it highlights errors that occur when using unsupported operations on strings.
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)
9 views3 pages

Class7 Datatype String

The document provides examples of string concatenation, indexing, and slicing in Python. It demonstrates various operations such as combining strings, accessing characters using positive and negative indexing, and extracting substrings through slicing. Additionally, it highlights errors that occur when using unsupported operations on strings.
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/ 3

In [4]: #string concatenation

firstname="saran"
lastname="raj"
fullname=firstname+lastname
print(fullname)
fullname=firstname+" "+lastname
print(fullname)
print(firstname,lastname)
fullname=firstname-lastname

saranraj
saran raj
saran raj
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[4], line 9
7 print(fullname)
8 print(firstname,lastname)
----> 9 fullname=firstname-lastname

TypeError: unsupported operand type(s) for -: 'str' and 'str'

In [6]: firstname="saran"
lastname="raj"
fullname=firstname*lastname
print(fullname)

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[6], line 3
1 firstname="saran"
2 lastname="raj"
----> 3 fullname=firstname*lastname
4 print(fullname)

TypeError: can't multiply sequence by non-int of type 'str'

In [8]: firstname="saran"
result=firstname*4
print(result)

saransaransaransaran

In [14]: #printing the string statements


a="learning python"
b="gurukula"
print("you are " +a+" in "+b)
print("you are {a} in {b}".format(a=a,b=b))
print("you are {} in {}".format(a,b))

you are learning python in gurukula


you are learning python in gurukula
you are learning python in gurukula
In [16]: #string indexing
a= "hello"
a[0],a[1],a[2],a[3],a[4],

Out[16]: ('h', 'e', 'l', 'l', 'o')

In [17]: #string indexing - negative indexing


a= "hello"
a[-5],a[-1],a[-2],a[-3],a[-4]

Out[17]: ('h', 'o', 'l', 'l', 'e')

In [19]: #string slicing [start:end-1]


a= "hello"
a[0:4]
a[0:5]

Out[19]: 'hello'

In [22]: #string slicing [start:end-1]


a= "hello"
a[1:4]
a[1:5]

Out[22]: 'ello'

In [23]: #string slicing [start:end-1]


a= "hello"
a[:5]

Out[23]: 'hello'

In [24]: #string slicing [start:end-1]


a= "hello"
a[:]

Out[24]: 'hello'

In [25]: #negative slicing [start:end-1]


a= "hello"
a[-5:-1]

Out[25]: 'hell'

In [26]: #negative slicing [start:end-1]


a= "hello"
a[-5:]

Out[26]: 'hello'

In [27]: #negative slicing [start:end-1]


a= "hello"
a[:]
Out[27]: 'hello'

In [28]: # back progpagation is not possible


a[3:0]

Out[28]: ''

In [32]: # step argument [start:end:step]


a="happy learning"
a[0::2]
a[0::3]
a[0::1]
a[0::4]

Out[32]: 'hyan'

You might also like