Image

Anyone Can Code.





Image

String Methods and Built-in Functions

Sammarth Kumar



Below, you can see a table of various built-in functions and string methods. Click on any one to learn more about it.

S.No. Method Application
1 len() Returns the number of characters in a string
2 title() Returns each word with a capital letter at the start and lowercase for the rest of the letters.
3 lower() Returns all the the letters in the string in the lowercase form
4 upper() Returns all the the letters in the string in the uppercase form
5 count() Counts how many times a substring appears in a string for a given index range
6 find() Finds the first instance of a substring in a string for a given index range and returns the index value of where its first character is. If the substring isn't found, it returns -1
7 index() Same as find, but produces error when substring is not found.
8 endswith() Returns boolean of whether a string ends with a substring or not
9 startswith() Returns boolean of whether a string ends with a substring or not
10 isalnum() Returns boolean of whether all the characters in a string are only numbers and/or alphabets or not
11 islower() Returns boolean of whether all the characters in a string are lowercase or not
12 isupper() Returns boolean of whether all the characters in a string are uppercase or not
13 isspace() Returns boolean of whether all the characters in a string are whitespaces or not
14 istitle() Returns boolean of whether all the characters in a string are title case (first letter of each word uppercase) or not
15 lstrip() Removes whitespaces on the left hand side of the string
16 rstrip() Removes whitespaces on the right hand side of the string
17 strip() Removes whitespaces on both the left and right hand side of the string
18 replace() Relplaces all occurences of an old substring with a new substring
19 join() Returns a string wherein characters have been joined by a separator
20 partition() Returns a string wherein the characters have been partitioned into a tuple of three strings. The first string is the substring before the separator, the second is the separator, the third is the substring succeeding the separator. (click on the left link to see a demonstration)
21 split() Returns a string wherein the characters have been separated by a delimiter. The default delimiter is a whitespace

1. len()

1
2
string = "Q-Programming"
print(len(string))

Output

13


2. title()

1
2
string = "q programming"
print(string.title())

Output

Q Programming


3. lower()

1
2
string = "Q-PROGRAMMING"
print(string.lower())

Output

q-programming


4. upper()

1
2
string = "q-programming"
print(string.upper())

Output

Q-PROGRAMMING


5. count()

1
2
3
#count(substring, lower limit, upper limit)
string = "Welcome to the Q-Programming website!"
print(string.count("we", 0,35))

Output

1

Note that the count() method is case-sensitive i.e. the case of each character matters. Therefeore, we got one as an output even tho "We" was present in the beginning of the string.


6. find()

1
2
3
#find(substring, lower limit, upper limit)
string = "Welcome to the Q-Programming website!"
print(string.find("we", 0,35))

Output

29

Here we get the output as 29 because the first instance of "we" is at the the 29th index in the string.

1
2
3
#find(substring, lower limit, upper limit)
string = "Welcome to the Q-Programming website!"
print(string.find("x", 0,35))

Output

-1

Here we got the output as -1 because "x" is not present in the string.


7. index()

1
2
3
#index(substring, lower limit, upper limit)
string = "Welcome to the Q-Programming website!"
print(string.index("we", 0,35))

Output

29

Here we got the same output as the first case for find().

1
2
3
#index(substring, lower limit, upper limit)
string = "Welcome to the Q-Programming website!"
print(string.index("x", 0,35))

Output

Traceback (most recent call last):
  File "<string>", line 3, in <module>
ValueError: substring not found

As you can see, we get a ValueError when a substring is not found by the index() method.


8. endswith()

1
2
string = "Welcome to the Q-Programming website!"
print(string.endswith("!"))

Output

True

1
2
string = "Welcome to the Q-Programming website!"
print(string.endswith("a"))

Output

False


9. startswith()

1
2
string = "Welcome to the Q-Programming website!"
print(string.startswith("W"))

Output

True

1
2
string = "Welcome to the Q-Programming website!"
print(string.startswith("e"))

Output

False


10. isalnum()

1
2
string = "Welcome to the Q-Programming website"
print(string.isalnum())

Output

True

1
2
string = "Welcome to the Q-Programming website!"
print(string.isalnum())

Output

False

In the second case, the output was False because of the presence of "!", which is a special character.


11. islower()

1
2
string = "qprogramming"
print(string.islower())

Output

True

1
2
string = "Qprogramming"
print(string.islower())

Output

False


12. isupper()

1
2
string = "QPROGRAMMING"
print(string.isupper())

Output

True

1
2
string = "Qprogramming"
print(string.isupper())

Output

False


13. isspace()

1
2
string = "     "
print(string.isspace())

Output

True

1
2
string = "Q-Programming"
print(string.isspace())

Output

False


14. istitle()

1
2
string = "Welcome To QProgramming"
print(string.istitle())

Output

True

1
2
string = "welcome to qprogramming"
print(string.istitle())

Output

False


15. lstrip()

1
2
string = "   Q-Programming"
print(string.lstrip())

Output

Q-Programming


16. rstrip()

1
2
string = "Q-Programming   "
print(string.rstrip())

Output

Q-Programming


17. strip()

1
2
string = "   Q-Programming    "
print(string.strip())

Output

Q-Programming


18. replace()

1
2
3
#replace(original substring,new substring)
string = "Welcome to Q Programming"
print(string.replace(" ","-"))

Output

Welcome-to-Q-Programming


19. join()

1
2
3
#replace(original substring,new substring)
string = "Welcome to Q Programming"
print(string.replace(" ","-"))

Output

Q*P*r*o*g*r*a*m*m*i*n*g


20. partition()

1
2
3
#ssting.paritiion(separator)
string = "Welcome to Q-Programming!"
print(string.partition("to"))

Output

('Welcome ', 'to', ' Q-Programming!')


21. split()

1
2
3
#ssting.paritiion(delimiter)
string = "Welcome to Q-Programming!"
print(string.partition("to"))

Output

['Welcome ', ' Q-Programming!']

That's it for this tutorial! Scroll up and click on Next for the next tutorial!

For information on how you can use this information, please refer to our Terms of Use


Copyright © 2020, Q-Programming | Created by Arjun Sodhi, Balpreet Juneja and Sammarth Kumar | Developed and Designed by Sammarth Kumar