Welcome to deBUG.to Community where you can ask questions and receive answers from Microsoft MVPs and other experts in our community.
0 like 0 dislike
120 views
in Blog Post by 2 8 12
edited by

In this post, we will list most of common Python String Methods also we will learn How to Call & write Functions?

Python String Methods

  1. count()
  2. find()
  3. rfind()
  4. split()
  5. join()
  6. isalnum()
  7. isdigit()
  8. strip()
  9. lower()
  10. upper()
  11. swapcase()
  12. title()

  • Syntax: count()

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))
  • Syntax: find()

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')
  • Syntax: rfind()

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')
  • Syntax: split()

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)
  • Syntax: join()

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)
  • Syntax:isalnum()

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())
  • Syntax: isdigit()

Definition and Usage: It returns True if all the characters are digits, otherwise False.

value = '987'
print (value.isdigit())
  • Syntax: strip()

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()
  • Syntax: lower()

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())
  • Syntax: upper()

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())
  • Syntax: swapcase()

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())
  • Syntax: title()

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

by 1 7 14
0 0
Nice Article, Ala'a, Please don't forget to add a description for each function and minimize the images before uploading! Thanks!

If you don’t ask, the answer is always NO!
...