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

Lecture2 Py738

The document provides an overview of the string data type, including basic operations such as concatenation, length, indexing, and slicing. It also introduces string functions and conditional statements for programming tasks, along with practice exercises for users. Key concepts include manipulating strings and using conditional logic to grade students based on their marks.

Uploaded by

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

Lecture2 Py738

The document provides an overview of the string data type, including basic operations such as concatenation, length, indexing, and slicing. It also introduces string functions and conditional statements for programming tasks, along with practice exercises for users. Key concepts include manipulating strings and using conditional logic to grade students based on their marks.

Uploaded by

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

Strings

String is data type that stores sequence of characters.

Basic Operations

concatenation

“hello”+“world” convert to “helloworld”

length of str

len(str)
Indexing

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

str=“Apna_College”

str[0]is‘A’,str[1]is‘p’...

str[0]=‘B’#notallowed
Slicing
Accessing parts of a string

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

str=“ApnaCollege”

str[1:4]is“pna”

str[:4]is same as str[0:4]

str[1:]is same as str[1:len(str)]


Slicing
Negative Index

Apple
-5-4-3-2-1

str=“Apple”

str[-3:-1]is“pl”
StringFunctions
str=“Iamacoder.”

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

str.capitalize()#capitalizes1st char

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

str.find(word)#returns1s tindex of 1st occurrence

str.count(“am“)#counts the occurrence of substrinstring


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

WAP to find the occurrence of‘3’in a String.


ConditionalStatements
if-elif-else(SYNTAX)

if(condition):
Statement1
elif(condition):
Statement2
else:
StatementN
ConditionalStatements
Grade students based on mark

marks>=90,grade=“A”

90 > marks >= 80, grade

= “B”

80>marks>=70,grade=“C

” 70>marks,grade=“D”
Let‘sPractice
WAP to check if a number entered by the user is oddoreven.

WAP to find the greatest of 3number sentered by theuser.

WAP to check if a number is a multiple of 7ornot.

You might also like