Day String

Susan Sarandon
Release: 2024-11-21 09:02:09
Original
497 people have browsed it

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)

Copy after login

Output:

Madurai's Jigarthanda is very famous
Copy after login

Example 2:

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

print(Address)

Copy after login

Output:

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

Copy after login

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

Copy after login

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

Copy after login

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

Copy after login

Output:

p
r
i
t
h
a
Copy after login

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

Copy after login

Output:

p r i t h a 
Copy after login

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

Copy after login

Output:

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

Copy after login

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

source:dev.to
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template