Day String

Susan Sarandon
Susan SarandonOriginal
2024-11-21 09:02:09523browse

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

Output:

Madurai's Jigarthanda is very famous

Example 2:

Address = """no. 7, East Street, 
            Mela masi veedhi,
            Madurai 625002"""

print(Address)

Output:

no. 7, East Street, 
            Mela masi veedhi,
            Madurai 625002

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

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

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

Output:

p
r
i
t
h
a

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

Output:

p r i t h a 

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

Output:

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

The above is the detailed content of Day String. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn