Python Programming with Corey Schafer
Lecture 2 - Strings
• Setting variable
message = ‘Hello World’
print(message)
→ Hello World
*Text data i.e. Hello World is called string
*Setting good variable names is essential for better understanding i.e. we could set the variable
as ‘m’ instead of ‘message’ which would be valid but it would be difficult for others to
understand
• Setting variable with multiple words have _ in between
my_message = ‘Hello World’
print(my_message)
→ Hello World
• Single quote
message = ‘Bobby’s World’
print(message)
→ error
It is because of two ’ ’
Solution 1 of error
message = ‘Bobby\’s World’
print(message)
→ Bobby’s World
Solution 2 of error
message = “Bobby’s World”
print(message)
→ Bobby’s World
• Multi-line string
message = “““Bobby’s World was a
good cartoon”””
print(message)
→ Bobby’s World was a
good cartoon
• No. of characters in a string / length on the string
message = ‘Hello World’
print(len(message))
→ 11
It includes the space between the words
• Access string’s characters individually
message = ‘Hello World’
print(message[0]) / print(message[10]) / print(message[11])
→ H / d / error
First character starts at 0 and last is at 10 in this case
• Access string’s characters
message = ‘Hello World’
print(message[0:5])
→ Hello
First index is inclusive i.e. 0 while the last index is not i.e. 5
message = ‘Hello World’
print(message[6:11])
→ World
First index is inclusive i.e. 6 while the last index is not i.e. 11
• Access string’s characters - slicing
message = ‘Hello World’
print(message[:5])
→ Hello
If first index is not mentioned it automatically means 0
• Access string’s characters - slicing
message = ‘Hello World’
print(message[6:])
→ World
If last index is not mentioned it automatically means the last character
• Upper casing string’s characters
message = ‘Hello World’
print(message.upper())
→ HELLO WORLD
• Lower casing string’s characters
message = ‘Hello World’
print(message.lower())
→ hello world
• Counting certain characters appearing in the string
message = ‘Hello World’
print(message.count(‘Hello’))
→1
Hello (argument) appears one time in message
message = ‘Hello World’
print(message.count(‘o’))
→2
o (argument) appears three times in message
• Finding index on a character
message = ‘Hello World’
print(message.find(‘World’))
→6
6 is the index of the character
message = ‘Hello World’
print(message.find(‘Universe’))
→ -1
-1 shows that it cannot find it in the string
• Replace characters in a string
message = ‘Hello World’
message.replace(‘World’, ‘Universe’)
print(message)
→ Hello World
It is because it is not making the replacement in place
Solution 1 – setting a new variable
message = ‘Hello World’
new_message = message.replace(‘World’, ‘Universe’)
print(new_message)
→ Hello Universe
Solution 2 – with the same variable
message = ‘Hello World’
message = message.replace(‘World’, ‘Universe’)
print(message)
→ Hello Universe
• Adding multiple strings
greeting = ‘Hello’
name = ‘Michael’
message = greeting + name
print(message)
→ HelloMichael
There is no space in between
Solution – adding another string in between
greeting = ‘Hello’
name = ‘Michael’
message = greeting + ‘, ’ + name
print(message)
→ Hello, Michael
greeting = ‘Hello’
name = ‘Michael’
message = greeting + ‘, ’ + name + ‘. Welcome!’
print(message)
→ Hello, Michael. Welcome!
• Using placeholders {} for long/complex strings
greeting = ‘Hello’
name = ‘Michael’
message = ‘{}, {}. Welcome!’.format(greeting,name)
print(message)
→ Hello, Michael. Welcome!
• Using f strings for long/complex strings
greeting = ‘Hello’
name = ‘Michael’
message = f‘{greeting}, {name}. Welcome!’
print(message)
→ Hello, Michael. Welcome!
greeting = ‘Hello’
name = ‘Michael’
message = f‘{greeting}, {name.upper()}. Welcome!’
print(message)
→ Hello, MICHAEL. Welcome!
For uppercase name
• To know about all the attributes and methods available with a variable
name = ‘Michael’
print(dir(name))
• To know more about all string methods
print(help(str))
• To know more about a specific string method
print(help(str.lower))
→ lower(self, /) unbound builtins.str method
Return a copy of the string converted to lowercase