0% found this document useful (0 votes)
13 views15 pages

Unit 3 Strings

This document covers the concept of strings in Python, including their properties, operations such as concatenation, slicing, and formatting, as well as built-in functions like ord() and chr(). It explains string immutability, indexing, and comparison methods. Additionally, it introduces string formatting techniques using the % operator, f-strings, and the format() method.

Uploaded by

adityagite2006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views15 pages

Unit 3 Strings

This document covers the concept of strings in Python, including their properties, operations such as concatenation, slicing, and formatting, as well as built-in functions like ord() and chr(). It explains string immutability, indexing, and comparison methods. Additionally, it introduces string formatting techniques using the % operator, f-strings, and the format() method.

Uploaded by

adityagite2006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

UNIT 3: STRINGS

FE Prof.Bhakti Patil
Syllabus contents

 Strings and its Operations:


 concatenation, appending, multiplication and
slicing. Strings are immutable, strings
formatting operator, built in string methods
and functions.
 Slice operation, ord() and chr() functions, in
and not in operators, comparing strings,
Iterating strings, the string module.
Strings
String is a sequence of zero or more characters.

String may contain alphabets, numbers and special characters.

Usually strings are enclosed within a single quotes and double


quotes.

Strings are immutable data structure.

 Python has built in <class str> to handle strings

 Python don’t have char data type like C/C++

Example: str1=“Hello World”


How to access a string??
 Individual character of a string is
accessed by its index.
 Eg: str1=“Hello”
 str1[0]= “H” and str1[-4]= “E”
Characters H E L L O
in the
string
Forward 0 1 2 3 4
index
Backward -5 -4 -3 -2 -1
index
Slicing, Concatenation,
Repetition
 Slicing is used to extract a part of string
 Slicing operator :
 syntax- str1[start:end:step]
 Concatenation- joining two strings
together
 Concatenation operator + “abc”+”5”
 Multiplication/repetition- print the string
multiple times
 Multiplication operator * “abc”*3
String formatting operators
String formatting operator % is unique to
strings.
•Example:
print("My name is %s and I secured
%d marks in Python in %s sem” %
(“Bhakti”,92,”first”))
Output:
My name is Bhakti and I secured 92 marks
in Python
String formatters
 F-strings-letter ‘f’ precedes the string
and variable are mentioned in { }
 Eg: st1=“pune”
print(f“I like {st1}”)
 Format() method
 Eg: s1=“Python”
print(“I love {}”.format(s1))
 s2= “Java”
print(“I love {} and {}.format(s1,s2))
String functions
Substring functions
ord() and chr() – built in
functions
 Used to convert character into int and
vice versa
 Complementary to each other
 ord() returns unicode value of character
 chr() returns character representing
given unicode value
 Eg: print(ord(‘a’)) ------- 97
print(chr(97)) -------- a
String comparison
We can compare two strings using comparison
operators such as ==, !=, <,<=,>, >=
Python compares strings based on their
corresponding ASCII values.
Example of string comparison

str1="green"
str2="black“

print("Is both Equal:", str1==str2)

print("Is str1> str2:", str1>str2)

print("Is str1< str2:", str1<str2)


Some more functions

isIdentifier() Returns True if the string is a str= “Hello”


valid print(str.isidentifier())
Identifier OUTPUT
True

enumerate(st Returns an enumerate object str = "Hello world“


r) that lists the index and value print(list(enumerate(s
of all the characters in the tr)))
string as pairs
OUTPUT

[ (0, H'), (1, 'e), (2, ‘l'


(3, ‘l’), (4, '0'), (5, ‘ ‘ )
,
(6, 'W'), (7, '0'), (8,
'r‘), (9, ‘l‘), (10, ‘d‘) ]

You might also like