String
String
Strings in python are surrounded by either single quotation marks, or double quotation marks.
In [60]:
In [61]:
len(str1)
Out[61]:
26
In [62]:
In [63]:
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.
In [65]:
In [66]:
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]
In [71]:
#negative indexing
greet='hello'
greet[-5]
Out[71]:
'h'
In [72]:
---------------------------------------------------------------------------
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'
In [73]:
greet='hello'
type(greet)
Out[73]:
str
In [81]:
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'
In [85]:
str2[1:-1]
Out[85]:
In [86]:
str2[-1:1]
Out[86]:
''
In [88]:
str2[1:8:2]
Out[88]:
'ecm '
In [89]:
str2[-1:]
Out[89]:
'h'
In [ ]:
str2[:-1]
In [114]:
Out[114]:
42
In [111]:
i=1
Out[111]:
'0b1011001'
In [ ]:
str2[-1::-1]
In [95]:
str2[-3:-8:-2]
Out[95]:
'etN'
upper()
title()
capitalize()
In [115]:
Out[115]:
In [116]:
a.upper()
Out[116]:
In [129]:
hello students
welcome to nettech
In [117]:
In [118]:
B.title()
Out[118]:
In [119]:
B.capitalize()
Out[119]:
In [ ]: