0% found this document useful (0 votes)
5 views7 pages

Python String Basics and Operations

The document explains the string data type in programming, detailing how strings are created in Python using single, double, and triple quotes. It covers operations on strings such as concatenation, length calculation, indexing, and slicing, as well as using negative indices. Additionally, it includes an example of looping through a string to print each character.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views7 pages

Python String Basics and Operations

The document explains the string data type in programming, detailing how strings are created in Python using single, double, and triple quotes. It covers operations on strings such as concatenation, length calculation, indexing, and slicing, as well as using negative indices. Additionally, it includes an example of looping through a string to print each character.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

STRINGS

• String is data type that stores a sequence of characters.


• String is the collection of the characters surrounded by single quotes, double quotes, or
triple quotes.
• The computer does not understand the characters directly; instead, they process and
store all data as binary (0s and 1s).
CREATING STRING IN PYTHON
#Using single quotes

str1 = 'Hello Python'

print(str1)

#Using double quotes

str2 = "Hello Python"

print(str2)

#Using triple quotes

str3 = '''Triple quotes are generally used for

represent the multiline or

docstring'''

print(str3)
OPERATION ON STRINGS

• Concatenation:
“Hello” + “world” = “Helloworld”
• Length of a String:
• fruit = “Mango”
• len1 = len(fruit)
• print(“Mango is a” , len1 , “letter word.”)
INDEXING

H E L L O
0 1 2 3 4

str = “HELLO”
str[0] is ‘H’ , str[1] is ‘L’ ……
SLICING

• Accessing parts of a string


str[ starting_idx : ending_idx ] #ending idx is not included

str = “Damini”
str[ 1 : 3 ] is “am”
str[ : 3 ] is same as str[ 0 : 3]
str[ 1 : ] is same as str[ 1 : len(str) ]
#Negative index:

A P P L E
-5 -4 -3 -2 -1

str = “APPLE”
str[ -3 : -1 ] is “PL”

EXAMPLE:
fruit = “apple”
len1 = lan(fruit)
print(len1)
LOOP THROUGH A STRING

alphabets = "String is data type that stores a sequence of characters."


for i in alphabets:
print(i)

You might also like