In this post, we will list most of common Python String Methods also we will learn How to Call & write Functions?
Python String Methods
- count()
- find()
- rfind()
- split()
- join()
- isalnum()
- isdigit()
- strip()
- lower()
- upper()
- swapcase()
- title()
Definition and Usage: It return the number of times the values appears int the list.
the_string = 'this is the student Alaa'
the_list = [ 1, 2, 2, 3, 3, 3, 3, ]
the_tuple = (4, 4, 4, 4, 5, 5, 5, 5, 5,)
print (the_string.count('s'))
print (the_list.count(2))
print (the_tuple.count(4))
Definition and Usage: It used to return the lowest index value of the first occurrence of the substring from the input string; else it returns -1.
text = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit , sed do'
print text.find('do')
Definition and Usage: It finds the last occurrence of the specified value.
text = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit , sed do'
print text.rfind('do')
Definition and Usage: It split a string into a list where each word is a list item.
text = 'A, B, C'
string_to_list = text.split()
print(string_to_list)
Definition and Usage: It join all items in a tuple / list into a string, using a hash character as separator.
alphabet = 'A, B, C, D'
string_to_list = alphabet.split(',')
list_to_string ="#".join(string_to_list)
print (list_to_string)
Definition and Usage: It returns True if all the characters are alphanumeric, meaning alphabet letter (a-z) and numbers (0-9).
value = 'A987'
print (value.isalnum())
value = 'A9 87'
print (value.isalnum())
Definition and Usage: It returns True if all the characters are digits, otherwise False.
value = '987'
print (value.isdigit())
Definition and Usage: It remove spaces at the beginning and at the end of the string.
text = ' python Course '
print text.strip()
#Or
print text.lstrip()
#Or
print text.rstrip()
Definition and Usage: It converts all uppercase characters in a string into lowercase characters and returns it.
str = 'This is python Course'
print ('the lower case converted string is ' + str.lower())
Definition and Usage: It converts all lowercase characters in a string into uppercase characters and returns it.
str = 'This is python Course'
print ('the upper case converted string is ' + str.upper())
Definition and Usage: It converts all uppercase characters to lowercase and all lowercase characters to uppercase characters of the given string, and returns it.
str = 'This is python Course'
print ('the swap case converted string is' + str.swapcase())
Definition and Usage: It returns a string where the first character in every word is upper case.
str = 'This is python Course'
print ('the title case converted string is ' + str.title())
Functional programming In Python