startswith()

startswith(prefix, start = 0, end = len(str)-1): Check if the string starts with the given value

parameters of startwith

  • prefix: check if the string starts
  • start = 0: Need to check the starting index of the given prefix
  • end = len(str)-1: Need to check the given end index
phone_number ='+212123456789';
print(phone_number.startswith('34',1)) # False
print(phone_number.startswith('+212')) # True
print(phone_number.startswith('56', 8)) # True
print(phone_number.startswith('78', 10, 12)) # True

replace()

replace(old_value, new_value, count): replace a specific substring with a given new value

parameter

  • old_value: the specific substring to replace it
  • new_value: replace its new value with old_value
  • count: (optional) number, specifying the number of occurrences of the old value to be replaced. The default is all occurrences
country_code = '212'
user_phone_number = f'{country_code}6123452129'
# 2126123452129
print(user_phone_number)
# 066123452129
print(user_phone_number.replace(country_code, '06', 1))

title()

title(): This string method converts the first character of each word to uppercase

article_slug = '5-html-tags-that-almost-nobody-knows'
article_title = article_slug.replace('-','').title()
# 5 HTML Tags That Almost Nobody Knows
print(article_title)

upper(), lower()

upper(): Convert the characters of the string to uppercase
lower(): Convert the characters of the string to lowercase

full_name ='Aya Bouchiha'
print(full_name.upper()) # AYA BOUCHIHA
print(full_name.lower()) # aya bouchiha

find()

find(valueToSearch, startIndex=0, endIndex=len(str)): Returns the index of the first occurrence of the given substring. It is similar to index(), but it returns -1 instead of raising an exception

parameter

  • valueToSearch: the string to be searched
  • startIndex = 0: the index to start the search
  • endIndex = len(str): the index to end the search
message ='Tanger is a beautiful city'
address ='Tanger, Morocco'
language ='en-us'
print(message.find('beautiful')) # 12
print(address.find('Morocco', len(address)-7)) # 8
print(language.find('en', 0, 2)) # 0
print(language.find('fr', 0, 2)) # -1

Generalize

  • startswith: Check if the string starts with the given value
  • title: Convert the first character of each word to uppercase
  • replace: replace a specific substring with a given new value
  • upper, lower: convert the characters of the string to uppercase and lowercase
  • find: Returns the index of the first occurrence of the given substring
Likes(0)

Comment list count 0 Comments

No Comments