DEV Community

Lakshmi Pritha Nadesan
Lakshmi Pritha Nadesan

Posted on

Day 5 - String

String datatype:
In Python, a string is a sequence of characters enclosed within either single quotes (') or double quotes (").

Python is a dynamically typed programming language.

Example:
x = 10 # 'x' is an integer
x = "Hello" # 'x' is a string

A string enclosed within three consecutive single quotes (''') or three consecutive double quotes (""").

Example 1:

name = 'Madurai'

city = '''Madurai's Jigarthanda is very famous'''
print(city)

Enter fullscreen mode Exit fullscreen mode

Output:

Madurai's Jigarthanda is very famous
Enter fullscreen mode Exit fullscreen mode

Example 2:

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 Python, everything is an object.
Every object has its own memory space.

Example:

name = 'pritha'
degree = 'B.E.,'
height = 170
sunday = False
print(id(name))
print(id(degree))
print(id(height))
print(id(sunday))

Enter fullscreen mode Exit fullscreen mode

name,degree,height,sunday these are the objects.
In Python, the id() function returns the unique memory address of the object passed to it.

Output:

130349764057584
130349766012592
11759304
10654592

Enter fullscreen mode Exit fullscreen mode

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 = 'pritha'

print(name[0])
print(name[1])
print(name[2])
print(name[3])
print(name[4])
print(name[5])

Enter fullscreen mode Exit fullscreen mode

Output:

p
r
i
t
h
a
Enter fullscreen mode Exit fullscreen mode

Example 2:

name = 'pritha'

print(name[0],end=' ')
print(name[1],end=' ')
print(name[2],end=' ')
print(name[3],end=' ')
print(name[4],end=' ')
print(name[5],end=' ')

Enter fullscreen mode Exit fullscreen mode

Output:

p r i t h a 
Enter fullscreen mode Exit fullscreen mode

Example 3:

name = 'kavitha'

# first letter
print(name[0])

#last letter
print(name[6])

#first letter 'g'
if name[0] == 'k':
    print("yes starts with k")

#last letter 'a'
if name[6] == '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=' ')
print(name[4], end=' ')
print(name[5], end=' ')
print(name[6], end=' ')

#middle letter 
length = len(name) 
print(name[length//2])

Enter fullscreen mode Exit fullscreen mode

Output:

k
a
yes starts with k
yes ends with a
k a v i t h a i

Enter fullscreen mode Exit fullscreen mode

Image of AssemblyAI tool

Challenge Submission: SpeechCraft - AI-Powered Speech Analysis for Better Communication

SpeechCraft is an advanced real-time speech analytics platform that transforms spoken words into actionable insights. Using cutting-edge AI technology from AssemblyAI, it provides instant transcription while analyzing multiple dimensions of speech performance.

Read full post

Top comments (0)

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay