Topics:
1. String Methods
2. String Slicing
3. Multi-line Strings
4. Find/Replace
5. In/Not in Comparisons
6. String Variable formatting
7. User Input
8. Task - Create a distance converter converting Km to miles
9. Isalpha() Method
10. Isdigit() Method
11. Isalnum() Method
12. Alphabet and Number ranges
1. String Methods
>>Return to Menu
message = "I love Python coding classes"
#Normal string
print(message)
#To Lowercase
print(message.lower())
#To Uppercase
print(message.upper())
#First char to Uppercase only
print(message.capitalize())
#First char of each Word to uppercase
print(message.title())
#Length of string
print(len(message))
#Number of times char(s) appear in string
print(message.count('o'))
2. String Slicing
>>Return to Menu
message = "I love Python always"
#The first character in the string.
print(message[0])
#The sixth character in the string.
print(message[5])
#The last character in the string.
print(message[-1])
#The third from the last character in the string.
print(message[-3])
#Everything from the forth character in the string.
print(message[3:])
#Everything from the fifth character to the tenth character (inclusive) in the string.
print(message[4:10])
#Everything from the first character to the ninth character (inclusive) in the string.
print(message[:9])
#Everything from the first character to the tenth character (inclusive) with a step of 2.
print(message[:10:2])
#Everything from the tenth character to the first character (inclusive) backwards.
print(message[9::-1])
#Everything from the last character to the fifth character (inclusive) backwards, with a step of 2.
print(message[-1:5:-2])
#Everything from the last character to the first character backwards (reverse).
print(message[-1::-1])
3. Multi-line Strings
>>Return to Menu
message="""Dear Bob,
I hope you are doing fine.
Remember to take out the trash.
Love you!
Regards,
Mum"""
print(message)
4. Find/Replace
>>Return to Menu
message = "I love Python coding classes"
#Find the index position of char 'y' in message
print(message.find('y'))
#Find the index position of word 'classes' in message
print(message.find('classes'))
#Replace 'I' with 'We' in message
message = message.replace('I', 'We')
print(message)
#Replace 'Python' with 'Javascript' in message
message = message.replace('Python', 'Javascript')
print(message)
5. In/Not In Comparisons
>>Return to Menu
message = "I love Python coding classes"
#Find out if word 'coding' is found in message
print('coding' in message)
#Find out if word 'raining' is found in message
print('raining' in message)
#Find out if word 'coding' is not found in message
print('coding' not in message)
#Find out if word 'raining' is not found in message
print('raining' not in message)
6. String Variable formatting
>>Return to Menu
house_number = 3
street = "creadle court avenue"
city = "houston"
state = "texas"
country = "usa"
message = f"I live in House {house_number}, {street.title()}, {city.capitalize()}, {state.capitalize()}, {country.upper()}."
print(message)
7. User Input
>>Return to Menu
name = input('What is your name? ')
print(f'Your name is {name}.')
age = input('How old are you? ')
print(f'You are {age} years old!')
add = input('Find how old you will be in x years time. x= ')
print(f'In {add} years time, you will be {int(age) + int(add)} years old.')
8. Task - Create a distance converter converting Km to miles
>>Return to Menu
Task:
# - Create a distance converter converting Km to miles
# - Take two inputs from user: Their first name and the distance in km
# - Print: Greet user by name and show km, and mile values
# - 1 mile is 1.609 kilometers
Solution:
name = input('Enter your name: ')
distance_km = input('Enter distance in km: ')
distance_mi = float(distance_km)/1.609
print(f'Hello {name.capitalize()}, you have covered {distance_km}km which is the same as {round(distance_mi,2)} miles.')
9. Isalpha() Method
>>Return to Menu
Isalpha() String Method returns True if the string contains at least one character of only alphabets (a to z, A-Z) else returns False.
#isalpha Method
is_true = 'abc'.isalpha()
print(is_true)
is_true = 'abc123'.isalpha()
print(is_true)
is_true = 'This is a string'.isalpha()
print(is_true)
a = 'Python'
is_true = a.isalpha()
print(is_true)
b = 'Python3'
is_true = b.isalpha()
print(is_true)
c = 'Python!'
is_true = c.isalpha()
print(is_true)
10. Isdigit() Method
>>Return to Menu
Isdigit() String Method returns True if the string contains only numeric digits (0 to 9) else returns False.
#isdigit Method
is_true = '123'.isdigit()
print(is_true)
is_true = 'abc123'.isdigit()
print(is_true)
is_true = '123 45 6 789123'.isdigit()
print(is_true)
a = '0810000000'
is_true = a.isdigit()
print(is_true)
b = '123Python'
is_true = b.isdigit()
print(is_true)
c = '123!'
is_true = c.isdigit()
print(is_true)
11. Isalnum() Method
>>Return to Menu
Isalnum() String Method returns True if the string contains at least one character of alphanumeric characters (a to z, A-Z, 0 to 9) else returns False.
#isalnum Method
is_true = 'abc123'.isalnum()
print(is_true)
is_true = '123'.isalnum()
print(is_true)
is_true = 'abc'.isalnum()
print(is_true)
a = 'abc123#$$$'
is_true = a.isalnum()
print(is_true)
b = '123Python'
is_true = b.isalnum()
print(is_true)
c = '123!abc'
is_true = c.isalnum()
print(is_true)
12. Alphabet and Number ranges
>>Return to Menu
import string
a = string.ascii_lowercase
print(a)
a = list(string.ascii_lowercase)
print(a)
# help(string)
# ....
# DATA
# ascii_letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
# ascii_lowercase = 'abcdefghijklmnopqrstuvwxyz'
# ascii_uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
# digits = '0123456789'
# hexdigits = '0123456789abcdefABCDEF'
# octdigits = '01234567'
# printable = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~ \t\n\r\x0b\x0c'
# punctuation = '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
# whitespace = ' \t\n\r\x0b\x0c'
#End
Hope you enjoyed this! :) Follow me for more contents...
Get in Touch:
ifeanyiomeata.com
contact@ifeanyiomeata.com
Youtube: youtube.com/c/IfeanyiOmeata
Linkedin: linkedin.com/in/omeatai
Twitter: twitter.com/iomeata
Github: github.com/omeatai
Stackoverflow: stackoverflow.com/users/2689166/omeatai
Hashnode: hashnode.com/@omeatai
Medium: medium.com/@omeatai
© 2022