python session day-5 at payilagam (String)

kuhanraja_ar_1df623b8e2e

Kuhanraja A R

Posted on November 19, 2024

python session day-5 at payilagam (String)

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)
Enter fullscreen mode Exit fullscreen mode
output:
asd
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode
output:
India's most attractive city is chennai.
Enter fullscreen mode Exit fullscreen mode
Address = """no. 7, East Street, 
            Mela masi veedhi,
            Madurai 625002"""

print(Address)
Enter fullscreen mode Exit fullscreen mode
output:

no. 7, East Street, 
            Mela masi veedhi,
            Madurai 625002
Enter fullscreen mode Exit fullscreen mode

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))
Enter fullscreen mode Exit fullscreen mode
output:
124057086329888
124057086340784
11759400
10654560
Enter fullscreen mode Exit fullscreen mode

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])

Enter fullscreen mode Exit fullscreen mode
output:
R
A
J
A
Enter fullscreen mode Exit fullscreen mode

example 2

name = "RAJA"

print(name[0],end=' ')
print(name[1],end=' ')
print(name[2],end=' ')
print(name[3],end=' ')
Enter fullscreen mode Exit fullscreen mode

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],)
Enter fullscreen mode Exit fullscreen mode
output:

R
A
yes starts with R
yes ends with A
R A J A 
 J

Enter fullscreen mode Exit fullscreen mode

some of the string functions:
capitalize() Converts the first character to upper case

name='RAJA'
print(name.capitalize())

output:
Raja
Enter fullscreen mode Exit fullscreen mode

casefold() Converts string into lower case


name='RAJA'
print(name.casefold())

output:
raja
Enter fullscreen mode Exit fullscreen mode

center() Returns a centered string

name='RAJA'
print(name.center(8))

output:
  RAJA  
Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
kuhanraja_ar_1df623b8e2e
Kuhanraja A R

Posted on November 19, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related