Strings 2 (2 hours)
Class Recording
Lecture Notes
Old Script
Content
- Various String methods.
- Concept of Mutability and Immutability
Meanwhile when people are joining (5-7 mins)
- Instructor's welcome.
- Breaking ice with students.
- Casual conversation to bond.
title: Quiz-1
description:
duration: 60
card_type: quiz_card
Question
What will be the output of the following code?
Choices
title: String Methods
description:
duration: 2400
card_type: cue_card
String Methods
.split() method
- The str.split() function converts a string into a list of characters based on a splitting criteria.
Code:
Output:
Code:
Output:
.join() method
- Opposite of splitting is joining.
- We can use str.join() to do so.
Code:
Output:
Code:
Output:
- While providing an iterable to .join() you need to make sure that the contents of that iterable only contain strings. Else it will throw an error.
Code:
Output:
str() function
- We can use str() function to convert numbers to string.
Code:
Output:
.find() method
- The .find() method can be used to find a substring inside a string that we can call it on.
- This function returns the starting index of the first occourance of the substring that we are trying to find inside the string.
Code:
Output:
- Returns -1 if the substring is not present.
Code:
Output:
.replace() method
- The method .replace() can be used to replace a substring from the original string with the input we provide.
- This method takes in two arguments, first the substring from the original string and second the string we want to replace it with.
- It replaces the string we provide and returns a new string.
Code:
Output:
.count() method
- The method .count() can be used to count the number of occourances of a substring we provide inside a particular string.
Code:
Output:
Code:
Output:
title: Question-1
description:
duration: 480
card_type: cue_card
- Take a string as input.
- Convert the string to lowercase without using any inbuilt function.
Code:
Output:
title: Quiz-2
description:
duration: 60
card_type: quiz_card
Question
What will be the output of the following code?
Choices
title: Quiz-3
description:
duration: 60
card_type: quiz_card
Question
What will be the output of the following code?
Choices
title: Some more methods
description:
duration: 1800
card_type: cue_card
Some more methods -
.isdigit()
- .isdigit() returns True a character inside a string is a digit.
- This method works only for integers.
Code:
Output:
Code:
Output:
Code:
Output:
Code:
Output:
.isalpha()
- isalpha() returns True if a certain string only contains alphabets
Code:
Output:
Code:
Output:
Code:
Output:
.isupper() and .islower()
Code:
Output:
Code:
Output:
Code:
Output:
Code:
Output:
.isspace()
- Returns True if there is a space in the string.
Code:
Output:
Code:
Output:
title: Question-2
description:
duration: 400
card_type: cue_card
- Take a string as input.
- Replace all the space with underscore.
Code:
Output:
Code:
Output:
title: Quiz-4
description:
duration: 60
card_type: quiz_card
Question
What will be the output of the following code?
Choices
title: Quiz-5
description:
duration: 60
card_type: quiz_card
Question
What will be the output of the following code?
Choices
title: Glossary of string methods
description:
duration: 90
card_type: cue_card
Glossary of string methods -
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
title: Mutability and Immutability
description:
duration: 900
card_type: cue_card
Mutability and Immutability
Mutable: whose value can change.
Immutable: whose value can't change.
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Code:
Output:
Code:
Output:
It seems that integers are mutable but NO. originally in python we are just changing the memory reference of a to point to 4 instead of changing its value to 4.
Only 3 data types in python are mutable
Other data types such as strings and tuples(you are going to learn in upcoming lectures) are immutable.
id() function in python gives the memory address of the variable where it is stored
Code:
Output:
Code:
Output:
Code:
Output:
Code:
Output:
As lists are mutable, we can see that the same list created in the first cell is modified and the values are changed in the same memory location.
Code:
Output:
- We receive an error as strings are immutable and we cannot change the value in place.
- We can instead assign a new value to the same variable, which leads to the creation of a different memory space
Code:
Output:
Code:
Output: