Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
12 views

String

This document discusses strings in Python. It explains that strings are immutable sequences that can be defined using single, double or triple quotes. It also covers string indexing, slicing and built-in string methods like lower, upper, title and capitalize.

Uploaded by

Sanket Patil
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

String

This document discusses strings in Python. It explains that strings are immutable sequences that can be defined using single, double or triple quotes. It also covers string indexing, slicing and built-in string methods like lower, upper, title and capitalize.

Uploaded by

Sanket Patil
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

2/10/22, 5:55 PM String (1) - Jupyter Notebook

String is a sequence of character

Strings in python are surrounded by either single quotation marks, or double quotation marks.

In Python, string is an immutable sequence data type.

In [60]:

str1='This is a string in Python'


print(str1)

This is a string in Python

In [61]:

len(str1)

Out[61]:

26

In [62]:

str2="This is a string in Python"


print(str2)

This is a string in Python

In [63]:

#Multi-line strings must be embed in triple quotes, as shown below.

str1='''This is
the first
Multi-line string.
'''
print(str1)

This is
the first
Multi-line string.

In [64]:

str5="""This is
the second
Multi-line
string."""
print(str5)

This is
the second
Multi-line
string.

localhost:8888/notebooks/Downloads/String (1).ipynb 1/6


2/10/22, 5:56 PM String (1) - Jupyter Notebook

In [65]:

str1='Welcome to "Python Tutorial" at NetTech'


print(str1)

Welcome to "Python Tutorial" at NetTech

In [66]:

str2="Welcome to 'Python Tutorial' on at NetTech"


print(str2)

Welcome to 'Python Tutorial' on at NetTech

In [67]:

greet='Hello'
len(greet)
5
Out[67]:

In [69]:

#positive indexing
greet='hello'
greet[0]
Out[69]:

'h'

In [70]:

greet[5]
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-70-92e7014796c6> in <module>()
----> 1 greet[5]

IndexError: string index out of range

In [71]:

#negative indexing
greet='hello'
greet[-5]
Out[71]:

'h'

localhost:8888/notebooks/Downloads/String (1).ipynb 2/6


2/10/22, 5:56 PM String (1) - Jupyter Notebook

In [72]:

#unmutable object hence it is not possible to modify it.


greet='hello'
greet[0]='A'

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-72-b51ef677656f> in <module>()
1 #unmutable object hence it is not possible to modify it.
2 greet='hello'
----> 3 greet[0]='A'

TypeError: 'str' object does not support item assignment

In [73]:

greet='hello'
type(greet)
Out[73]:

str

In [81]:

str2="Welcome to 'Python Tutorial' at NetTech"


str2[-10]

Out[81]:

'a'

In [82]:

greet[1:5]
Out[82]:

'ello'

In [83]:

str2[0:7]

Out[83]:

'Welcome'

In [84]:

str2[4:9]

Out[84]:

'ome t'

localhost:8888/notebooks/Downloads/String (1).ipynb 3/6


2/10/22, 5:56 PM String (1) - Jupyter Notebook

In [85]:

str2[1:-1]
Out[85]:

"elcome to 'Python Tutorial' at NetTec"

In [86]:

str2[-1:1]
Out[86]:

''

In [88]:

str2="Welcome to 'Python Tutorial' on at NetTech"

str2[1:8:2]
Out[88]:

'ecm '

In [89]:

str2[-1:]
Out[89]:

'h'

In [ ]:

str2[:-1]

In [114]:

str2="Welcome to 'Python Tutorial' on at NetTech"


len(str2)

Out[114]:

42

In [111]:

i=1
Out[111]:

'0b1011001'

In [ ]:

str2[-1::-1]

localhost:8888/notebooks/Downloads/String (1).ipynb 4/6


2/10/22, 5:56 PM String (1) - Jupyter Notebook

In [95]:

str2[-3:-8:-2]
Out[95]:

'etN'

###String Functions lower()

upper()

title()

capitalize()

In [115]:

a = "My Name Is Sanvee"


a.lower()

Out[115]:

'my name is sanvee'

In [116]:

a.upper()
Out[116]:

'MY NAME IS SANVEE'

In [129]:

print("hello students \nwelcome to nettech")

hello students
welcome to nettech

In [117]:

B = "MY NAME IS SANVEE"

In [118]:

B.title()
Out[118]:

'My Name Is Sanvee'

In [119]:

B.capitalize()

Out[119]:

'My name is sanvee'

localhost:8888/notebooks/Downloads/String (1).ipynb 5/6


2/10/22, 5:56 PM String (1) - Jupyter Notebook

In [ ]:

localhost:8888/notebooks/Downloads/String (1).ipynb 6/6

You might also like