Strings
Strings in python are surrounded by either single quotation marks, or double quotation marks.
'hello' is the same as "hello".
You can display a string literal with the print() function:
example
name = 'varatharajan'
name = "varatharajan"
print(name)
print(name)
result
`
varatharajan
varatharajan
`
Multiline Strings
You can assign a multiline string to a variable by using three quotes:
example1:
city = '''Madurai's Jigarthanda is very famous'''
print(city)
result
Madurai's Jigarthanda is very famous
example2:
address = """3, kosakulam,
madurai - 17"""
print(address)
result
3, kosakulam,
madurai - 17
Objects
Python is an object oriented programming language.
Almost everything in Python is an object, with its properties and methods.
Every object has its own memory space.
In Python, the id() function returns the unique memory address of the object passed to it.
example :
name = 'Varatharajan'
degree = 'Bsc.Phy,'
height = 165
sunday = False
print(id(name))
print(id(degree))
print(id(height))
print(id(sunday))
result
1855594568496
1855594568304
1855593256304
140732337605512
1.String is immutable.
2.String is index-based
3.String starts from 0
Index/subscript:
A string is a sequence of characters, so indexing can be used to access individual characters.
example 1 :
name = 'varatharajan'
print(name[0])
print(name[1])
print(name[2])
print(name[3])
print(name[4])
print(name[5])
print(name[6])
print(name[7])
print(name[8])
print(name[9])
print(name[10])
print(name[11])
result
v
a
r
a
t
h
a
r
a
j
a
n
example 2 :
name = 'varatharajan'
print(name[0],end=' ')
print(name[1],end=' ')
print(name[2],end=' ')
print(name[3],end=' ')
print(name[4],end=' ')
print(name[5],end=' ')
print(name[6],end=' ')
result
v a r a t h a
example 3 :
name = 'varatharajan'
# first letter
print(name[0])
#last letter
print(name[11])
#first letter 'v'
if name[0] == 'v':
print("yes starts with v")
#last letter 'a'
if name[11] == 'n':
print("yes ends with n")
#all letters with single space in same line
print(name[0], end=' ')
print(name[1], end=' ')
print(name[2], end=' ')
print(name[3], end=' ')
print(name[4], end=' ')
print(name[5], end=' ')
print(name[6], end=' ')
print(name[7], end=' ')
print(name[8], end=' ')
print(name[9], end=' ')
print(name[10], end=' ')
print(name[11], end=' ')
#middle letter ??
length = len(name) #12
print(name[length//2])
result
v
n
yes starts with v
yes ends with n
v a r a t h a r a j a n a
Top comments (0)