Strings
Python Programming
Prof. Supriya S. Telsang
supriya.telsang@vit.edu
Department of Computer Engineering
BRACT’S, Vishwakarma Institute of Technology, Pune
(An Autonomous Institute affiliated to Savitribai Phule Pune University)
(NBA and NAAC accredited, ISO 9001:2015 certified)
Department of Engineering Sciences and Humanities, VIT, Pune
Operators: (2Hrs)
Arithmetic Operators, relational Operators, Logical Operators, bitwise operators,
other stray operators, Operator Precedence, Mathematical Functions, Trigonometric
Functions, Random Number Functions, Strings in python : subscript operator,
indexing, slicing a string, string methods
Learning Outcome:
Demonstrate mathematical and string manipulation functions in Python.
Department of Engineering Sciences and Humanities, VIT, Pune
Strings
• String is a sequence of characters. It may contain
alphabets, numbers and special characters.
• Strings are enclosed in single and double quotes.
• Strings are immutable in nature.
Examples:
‘hello’
“college”
Department of Engineering Sciences and Humanities, VIT, Pune
Subscript operator
Subscript operator [ ] is used to access elements of
sequence like strings, list and tuples.
char my_string[10];
my_string=“Hello:;
print(my_string[1])
Output: e
Department of Engineering Sciences and Humanities, VIT, Pune
Indexing
Individual characters of the string can be accessed using indexing.
Example:
char name[10];
name=“vit college”
print(name[0]) output: v
print(name[4]) output: c
print(name[8]) output: e
Department of Engineering Sciences and Humanities, VIT, Pune
Slicing
A segment of s string is called slice.
Slicing is used to return/select/slice the particular substring based on user
requirements.
Syntax: string_variablename [start : end]
Example:
Name=“vit college”
print(“first 5 characters are”,name[:5]) output:vit c
nd th
print(“2 to 5 character”, name[2:5]) output: it c
Print(“excluding first 5 characters”,name[5:]) output:ollege
Department of Engineering Sciences and Humanities, VIT, Pune
Example
Department of Engineering Sciences and Humanities, VIT, Pune
String methods
Department of Engineering Sciences and Humanities, VIT, Pune
Department of Engineering Sciences and Humanities, VIT, Pune
String Functions and Methods
Department of Engineering Sciences and Humanities, VIT, Pune
Department of Engineering Sciences and Humanities, VIT, Pune
Department of Engineering Sciences and Humanities, VIT, Pune
Department of Engineering Sciences and Humanities, VIT, Pune
Department of Engineering Sciences and Humanities, VIT, Pune
Department of Engineering Sciences and Humanities, VIT, Pune
Department of Engineering Sciences and Humanities, VIT, Pune
Department of Engineering Sciences and Humanities, VIT, Pune
Department of Engineering Sciences and Humanities, VIT, Pune
Department of Engineering Sciences and Humanities, VIT, Pune
Department of Engineering Sciences and Humanities, VIT, Pune
THANK YOU !!!
Department of Engineering Sciences and Humanities, VIT, Pune