Python Day4
Python Day4
master
Asabeneh
testimonial
History
1
contributor
Author:
Asabeneh Yetayeh
https://github.com/Asabeneh/30-Days-Of-Python/blob/master/04_Day_Strings/04_strings.md 1/15
7/30/22, 2:22 PM 30-Days-Of-Python/04_strings.md at master · Asabeneh/30-Days-Of-Python · GitHub
Day 4
Strings
Creating a String
String Concatenation
Escape Sequences in Strings
String formatting
Old Style String Formatting (% Operator)
New Style String Formatting (str.format)
String Interpolation / f-Strings (Python 3.6+)
Python Strings as Sequences of Characters
Unpacking Characters
Accessing Characters in Strings by Index
Slicing Python Strings
Reversing a String
Skipping Characters While Slicing
String Methods
💻 Exercises - Day 4
Day 4
Strings
https://github.com/Asabeneh/30-Days-Of-Python/blob/master/04_Day_Strings/04_strings.md 2/15
7/30/22, 2:22 PM 30-Days-Of-Python/04_strings.md at master · Asabeneh/30-Days-Of-Python · GitHub
Text is a string data type. Any data type written as text is a string. Any data under single,
double or triple quote are strings. There are different string methods and built-in functions
to deal with string data types. To check the length of a string use the len() method.
Creating a String
print(len(letter)) # 1
greeting = 'Hello, World!' # String could be made using a single or double quote,"H
print(greeting) # Hello, World!
print(len(greeting)) # 13
print(sentence)
Multiline string is created by using triple single (''') or triple double quotes ("""). See the
example below.
print(multiline_string)
print(multiline_string)
String Concatenation
We can connect strings together. Merging or connecting strings is called concatenation.
See the example below:
first_name = 'Asabeneh'
last_name = 'Yetayeh'
print(len(first_name)) # 8
print(len(last_name)) # 7
https://github.com/Asabeneh/30-Days-Of-Python/blob/master/04_Day_Strings/04_strings.md 3/15
7/30/22, 2:22 PM 30-Days-Of-Python/04_strings.md at master · Asabeneh/30-Days-Of-Python · GitHub
print(len(full_name)) # 16
Now, let us see the use of the above escape sequences with examples.
print('I hope everyone is enjoying the Python Challenge.\nAre you ?') # line break
print('Day 1\t3\t5')
print('Day 2\t3\t5')
print('Day 3\t3\t5')
print('Day 4\t3\t5')
# output
Are you ?
Day 1 5 5
Day 2 6 20
Day 3 5 23
Day 4 1 35
String formatting
https://github.com/Asabeneh/30-Days-Of-Python/blob/master/04_Day_Strings/04_strings.md 4/15
7/30/22, 2:22 PM 30-Days-Of-Python/04_strings.md at master · Asabeneh/30-Days-Of-Python · GitHub
In Python there are many ways of formatting strings. In this section, we will cover some of
them.
The "%" operator is used to format a set of variables enclosed in a "tuple" (a fixed
size list), together with a format string, which contains normal text together with
"argument specifiers", special symbols like "%s", "%d", "%f", "%.number of digitsf".
# Strings only
first_name = 'Asabeneh'
last_name = 'Yetayeh'
language = 'Python'
print(formated_string)
radius = 10
pi = 3.14
area = pi * radius ** 2
first_name = 'Asabeneh'
last_name = 'Yetayeh'
language = 'Python'
print(formated_string)
a = 4
b = 3
https://github.com/Asabeneh/30-Days-Of-Python/blob/master/04_Day_Strings/04_strings.md 5/15
7/30/22, 2:22 PM 30-Days-Of-Python/04_strings.md at master · Asabeneh/30-Days-Of-Python · GitHub
# output
4 + 3 = 7
4 - 3 = 1
4 * 3 = 12
4 / 3 = 1.33
4 % 3 = 1
4 // 3 = 1
4 ** 3 = 64
radius = 10
pi = 3.14
area = pi * radius ** 2
Another new string formatting is string interpolation, f-strings. Strings start with f and we
can inject the data in their corresponding positions.
a = 4
b = 3
Unpacking Characters
https://github.com/Asabeneh/30-Days-Of-Python/blob/master/04_Day_Strings/04_strings.md 6/15
7/30/22, 2:22 PM 30-Days-Of-Python/04_strings.md at master · Asabeneh/30-Days-Of-Python · GitHub
language = 'Python'
print(a) # P
print(b) # y
print(c) # t
print(d) # h
print(e) # o
print(f) # n
In programming counting starts from zero. Therefore the first letter of a string is at zero
index and the last letter of a string is the length of a string minus one.
language = 'Python'
first_letter = language[0]
print(first_letter) # P
second_letter = language[1]
print(second_letter) # y
last_index = len(language) - 1
last_letter = language[last_index]
print(last_letter) # n
If we want to start from right end we can use negative indexing. -1 is the last index.
language = 'Python'
last_letter = language[-1]
print(last_letter) # n
second_last = language[-2]
print(second_last) # o
https://github.com/Asabeneh/30-Days-Of-Python/blob/master/04_Day_Strings/04_strings.md 7/15
7/30/22, 2:22 PM 30-Days-Of-Python/04_strings.md at master · Asabeneh/30-Days-Of-Python · GitHub
language = 'Python'
print(first_three) #Pyt
last_three = language[3:6]
print(last_three) # hon
# Another way
last_three = language[-3:]
print(last_three) # hon
last_three = language[3:]
print(last_three) # hon
Reversing a String
It is possible to skip characters while slicing by passing step argument to slice method.
language = 'Python'
pto = language[0:6:2] #
print(pto) # Pto
String Methods
There are many string methods which allow us to format strings. See some of the string
methods in the following example:
print(challenge.count('y')) # 3
https://github.com/Asabeneh/30-Days-Of-Python/blob/master/04_Day_Strings/04_strings.md 8/15
7/30/22, 2:22 PM 30-Days-Of-Python/04_strings.md at master · Asabeneh/30-Days-Of-Python · GitHub
print(challenge.count('y', 7, 14)) # 1,
print(challenge.count('th')) # 2`
print(challenge.endswith('on')) # True
print(challenge.endswith('tion')) # False
expandtabs(): Replaces tab character with spaces, default tab size is 8. It takes tab size
argument
challenge = 'thirty\tdays\tof\tpython'
find(): Returns the index of the first occurrence of a substring, if not found returns -1
print(challenge.find('y')) # 16
print(challenge.find('th')) # 17
rfind(): Returns the index of the last occurrence of a substring, if not found returns -1
print(challenge.rfind('y')) # 5
print(challenge.rfind('th')) # 1
first_name = 'Asabeneh'
last_name = 'Yetayeh'
age = 250
job = 'teacher'
country = 'Finland'
radius = 10
pi = 3.14
https://github.com/Asabeneh/30-Days-Of-Python/blob/master/04_Day_Strings/04_strings.md 9/15
7/30/22, 2:22 PM 30-Days-Of-Python/04_strings.md at master · Asabeneh/30-Days-Of-Python · GitHub
area = pi * radius ** 2
index(): Returns the lowest index of a substring, additional arguments indicate starting
and ending index (default 0 and string length - 1). If the substring is not found it raises
a valueError.
sub_string = 'da'
print(challenge.index(sub_string)) # 7
sub_string = 'da'
print(challenge.rindex(sub_string)) # 8
challenge = 'ThirtyDaysPython'
print(challenge.isalnum()) # True
challenge = '30DaysPython'
print(challenge.isalnum()) # True
print(challenge.isalnum()) # False
isalpha(): Checks if all string elements are alphabet characters (a-z and A-Z)
challenge = 'ThirtyDaysPython'
print(challenge.isalpha()) # True
https://github.com/Asabeneh/30-Days-Of-Python/blob/master/04_Day_Strings/04_strings.md 10/15
7/30/22, 2:22 PM 30-Days-Of-Python/04_strings.md at master · Asabeneh/30-Days-Of-Python · GitHub
num = '123'
print(num.isalpha()) # False
print(challenge.isdecimal()) # False
challenge = '123'
print(challenge.isdecimal()) # True
challenge = '\u00B2'
print(challenge.isdigit()) # False
isdigit(): Checks if all characters in a string are numbers (0-9 and some other unicode
characters for numbers)
challenge = 'Thirty'
print(challenge.isdigit()) # False
challenge = '30'
print(challenge.isdigit()) # True
challenge = '\u00B2'
print(challenge.isdigit()) # True
isnumeric(): Checks if all characters in a string are numbers or number related (just like
isdigit(), just accepts more symbols, like ½)
num = '10'
print(num.isnumeric()) # True
num = '\u00BD' # ½
print(num.isnumeric()) # True
num = '10.5'
print(num.isnumeric()) # False
isidentifier(): Checks for a valid identifier - it checks if a string is a valid variable name
challenge = '30DaysOfPython'
challenge = 'thirty_days_of_python'
print(challenge.isidentifier()) # True
https://github.com/Asabeneh/30-Days-Of-Python/blob/master/04_Day_Strings/04_strings.md 11/15
7/30/22, 2:22 PM 30-Days-Of-Python/04_strings.md at master · Asabeneh/30-Days-Of-Python · GitHub
print(challenge.islower()) # True
print(challenge.islower()) # False
print(challenge.isupper()) # False
print(challenge.isupper()) # True
strip(): Removes all given characters starting from the beginning and end of the string
https://github.com/Asabeneh/30-Days-Of-Python/blob/master/04_Day_Strings/04_strings.md 12/15
7/30/22, 2:22 PM 30-Days-Of-Python/04_strings.md at master · Asabeneh/30-Days-Of-Python · GitHub
print(challenge.startswith('thirty')) # True
print(challenge.startswith('thirty')) # False
🌕 You are an extraordinary person and you have a remarkable potential. You have just
completed day 4 challenges and you are four steps a head in to your way to greatness.
Now do some exercises for your brain and muscles.
💻 Exercises - Day 4
1. Concatenate the string 'Thirty', 'Days', 'Of', 'Python' to a single string, 'Thirty Days Of
Python'.
2. Concatenate the string 'Coding', 'For' , 'All' to a single string, 'Coding For All'.
3. Declare a variable named company and assign it to an initial value "Coding For All".
4. Print the variable company using print().
5. Print the length of the company string using len() method and print().
6. Change all the characters to uppercase letters using upper() method.
7. Change all the characters to lowercase letters using lower() method.
8. Use capitalize(), title(), swapcase() methods to format the value of the string Coding
For All.
9. Cut(slice) out the first word of Coding For All string.
https://github.com/Asabeneh/30-Days-Of-Python/blob/master/04_Day_Strings/04_strings.md 13/15
7/30/22, 2:22 PM 30-Days-Of-Python/04_strings.md at master · Asabeneh/30-Days-Of-Python · GitHub
10. Check if Coding For All string contains a word Coding using the method index, find or
other methods.
11. Replace the word coding in the string 'Coding For All' to Python.
12. Change Python for Everyone to Python for All using the replace method or other
methods.
13. Split the string 'Coding For All' using space as the separator (split()) .
14. "Facebook, Google, Microsoft, Apple, IBM, Oracle, Amazon" split the string at the
comma.
15. What is the character at index 0 in the string Coding For All.
16. What is the last index of the string Coding For All.
17. What character is at index 10 in "Coding For All" string.
18. Create an acronym or an abbreviation for the name 'Python For Everyone'.
19. Create an acronym or an abbreviation for the name 'Coding For All'.
20. Use index to determine the position of the first occurrence of C in Coding For All.
21. Use index to determine the position of the first occurrence of F in Coding For All.
22. Use rfind to determine the position of the last occurrence of l in Coding For All People.
23. Use index or find to find the position of the first occurrence of the word 'because' in
the following sentence: 'You cannot end a sentence with because because because is a
conjunction'
24. Use rindex to find the position of the last occurrence of the word because in the
following sentence: 'You cannot end a sentence with because because because is a
conjunction'
25. Slice out the phrase 'because because because' in the following sentence: 'You cannot
end a sentence with because because because is a conjunction'
26. Find the position of the first occurrence of the word 'because' in the following
sentence: 'You cannot end a sentence with because because because is a conjunction'
27. Slice out the phrase 'because because because' in the following sentence: 'You cannot
end a sentence with because because because is a conjunction'
28. Does ''Coding For All' start with a substring Coding?
29. Does 'Coding For All' end with a substring coding?
30. ' Coding For All ' , remove the left and right trailing spaces in the given string.
31. Which one of the following variables return True when we use the method
isidentifier():
30DaysOfPython
thirty_days_of_python
32. The following list contains the names of some of python libraries: ['Django', 'Flask',
'Bottle', 'Pyramid', 'Falcon']. Join the list with a hash with space string.
https://github.com/Asabeneh/30-Days-Of-Python/blob/master/04_Day_Strings/04_strings.md 14/15
7/30/22, 2:22 PM 30-Days-Of-Python/04_strings.md at master · Asabeneh/30-Days-Of-Python · GitHub
33. Use the new line escape sequence to separate the following sentences.
radius = 10
8 + 6 = 14
8 - 6 = 2
8 * 6 = 48
8 / 6 = 1.33
8 % 6 = 2
8 // 6 = 1
8 ** 6 = 262144
🎉 CONGRATULATIONS ! 🎉
<< Day 3 | Day 5 >>
https://github.com/Asabeneh/30-Days-Of-Python/blob/master/04_Day_Strings/04_strings.md 15/15