string data type :
In python , A string is a sequence of characters. It treats anything inside quotes as a string.
example:
a='asd'
print(a)
output:
asd
whatever we are giving inside a single or double quotes.It treats everything as a character.
it displays as it is inside a string.
If a string as already quotes in it. for some grammar purpose.like,
India's most attractive city is chennai.
we can't give string normally, we have to give like this
city = '''India's most attractive city is chennai.'''
print(city)
output:
India's most attractive city is chennai.
Address = """no. 7, East Street,
Mela masi veedhi,
Madurai 625002"""
print(Address)
output:
no. 7, East Street,
Mela masi veedhi,
Madurai 625002
In this scenario, we can also use (triple) double quotes for string denotion.
python denotes everything as an object, Every object has its own memory space.
name = 'RAJA'
degree = 'B.tech'
height = 173
Payilagam_trainee = TRUE
print(id(name))
print(id(degree))
print(id(height))
print(id(Payilagam_trainee))
output:
124057086329888
124057086340784
11759400
10654560
the id() function returns the unique memory address of the object passed to it.
String Indexing:
String indexing can be used to access individual characters in the string.
example:
name = "RAJA"
print(name[0])
print(name[1])
print(name[2])
print(name[3])
output:
R
A
J
A
example 2
name = "RAJA"
print(name[0],end=' ')
print(name[1],end=' ')
print(name[2],end=' ')
print(name[3],end=' ')
output:
R A J A
end=' ' it indicates to continue in a whitespace not in a new line.
name = 'RAJA'
# first letter
print(name[0])
#last letter
print(name[3])
#first letter 'R'
if name[0] == 'R':
print("yes starts with R")
#last letter 'A'
if name[3] == 'A':
print("yes ends with A")
#all letters with single space in same line
print(name[0], end=' ')
print(name[1], end=' ')
print(name[2], end=' ')
print(name[3], end=' ')
#middle letter
length = len(name)
print("\n",name[length//2],)
output:
R
A
yes starts with R
yes ends with A
R A J A
J
some of the string functions:
capitalize() Converts the first character to upper case
name='RAJA'
print(name.capitalize())
output:
Raja
casefold() Converts string into lower case
name='RAJA'
print(name.casefold())
output:
raja
center() Returns a centered string
name='RAJA'
print(name.center(8))
output:
RAJA
Top comments (0)