0% found this document useful (0 votes)
14 views9 pages

Lecture2 Py

The document provides an overview of strings as a data type in programming, detailing basic operations such as concatenation, indexing, and slicing. It also covers string functions and conditional statements, including examples of how to implement them in practice. Additionally, it includes exercises for users to apply their understanding of these concepts.

Uploaded by

anushkaewms
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)
14 views9 pages

Lecture2 Py

The document provides an overview of strings as a data type in programming, detailing basic operations such as concatenation, indexing, and slicing. It also covers string functions and conditional statements, including examples of how to implement them in practice. Additionally, it includes exercises for users to apply their understanding of these concepts.

Uploaded by

anushkaewms
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

lege
Col
String is data type that stores a sequence of characters.

pna
Basic Operations

concatenation
A BT3YK3TK^Z3ROTK3PGGZG3NGO

BZ3YK3ZGH3QK3HGXGHGX3QK3

YVGIK3QG3MGV3GGPG_KMG

“hello” + “world” “helloworld”

length of str

len(str)
Indexing

Apna_College

ge
0 1 2 3 4 5 6 7 8 9 10 11

olle
na
str = “Apna_College”
C
Ap
str[0] is ‘A’, str[1] is ‘p’ ...

str[0] = ‘B’ #not allowed


Slicing
Accessing parts of a string

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

ge
str = “ApnaCollege”

str[ 1 : 4 ] is “pna”

olle
na C
str[ : 4 ] is same as str[ 0 : 4]

Ap
str[ 1 : ] is same as str[ 1 : len(str) ]
Slicing
Negative Index

e
Apple

lleg
o
-5 -4 -3 -2 -1

na C
p
str = “Apple”

A
str[ -3 : -1 ] is “pl”
String Functions
str = “I am a coder.”

lege
str.endsWith(“er.“) #returns true if string ends with substr

Col
a
str.capitalize( ) #capitalizes 1st char

Apn
str.replace( old, new ) #replaces all occurrences of old with new

str.find( word ) #returns 1st index of 1st occurrence

str.count(“am“) #counts the occurrence of substr in string


Let‘s Practice
WAP to input user’s first name & print its length.

lege
Col
na
WAP to find the occurrence of ‘$’ in a String.

p
A
Conditional Statements
if-elif-else (SYNTAX)

ge
if(condition) :
Statement1

olle
a C
elif(condition):

n
Ap
Statement2
else:
StatementN
Conditional Statements
Grade students based on marks

ge
marks >= 90, grade = “A”

olle
C
90 > marks >= 80, grade = “B”

pna
80 > marks >= 70, grade = “C”

A
70 > marks, grade = “D”
Let‘s Practice
WAP to check if a number entered by the user is odd or even.

lege
Col
WAP to find the greatest of 3 numbers entered by the user.

pna
A
WAP to check if a number is a multiple of 7 or not.

You might also like