Sitemap

Predefined Functions in string Data type

6 min readSep 22, 2023

--

Hello Everyone Vrushali here again, Today we are going to discuss about the built-in function in string datatype.

How Many Built-in function in string datatype ?

Below is the built in function in string data type. we can shows those function using the following function:

print(dir(str)):

Here, dir() is a powerful inbuilt function in Python, which returns list of the attributes and methods of any object like functions , modules, strings, lists, dictionaries etc.

Press enter or click to view image in full size

let’s start!

Following are the predefined string function in python. we will start with capitalize().

1. capitalize() :

In capitalize() methods returns string where the 1st character is in upper case, and rest of in lower case.

We know that python is easy and simple to learn hence python gives us Python help() function to get the documentation of specified module, class, function, variables etc. we can get the more information using this help() function.

Press enter or click to view image in full size

Syntax :

string.capitalize()

Press enter or click to view image in full size

In above image we declare session variable having a “fifth” value, then we use the capitalize() function. then here see the output from fifth value change to Fifth means that using capitalize method we can change the first character of string in capitalize form.

2. casefold():

The casefold() method returns a string where all the characters are lower case.

syntax:

string.casefold()

Press enter or click to view image in full size

In above example subject variable holds a value “GeoGrapHy”, we use the casefold function with subject then the output is “geography”, this means using a casefold all the characters are in the string lower case.

3.center():

The center() method will center align the string, using a specified character, space is default as the fill character.

Syntax:

string.center(lenght, character)

Press enter or click to view image in full size

4. count():

String count() function is an inbuilt function in Python programming language that returns the number of occurrences of a substring in the given string.

Syntax:

string.count(substring)

Press enter or click to view image in full size

5. endswith():

Python String endswith() method returns True if a string ends with the given suffix, otherwise returns False.

Syntax :

string.endswith(suffix)

  • suffix: Suffix is nothing but a string that needs to be checked.
Press enter or click to view image in full size

6. expandtabs():

In expandtabs() method specifies the amount od space to be substituted with the “\t” symbol in the string.

Syntax:

string.expandtabs(tab_size)

. tab_size = specifies the size that is to be replaced with “/t” in string. By default space size is 8.

Press enter or click to view image in full size

7. find()

string find() method returns the lowest index or first occurrence of the substring if it is found in a given string. If it is not found, then it returns -1.

Press enter or click to view image in full size

In above example we declare the variable is ‘u’. the variable ‘u’ is holding the value is “Hello world”.

Here, we discuss the input line In[9]: we give the parameter “d” it will be return the lowest index that is 10, and in In[11]: character “z” is not present then it will return the value as “-1”.

8. format():

string format() function has been introduced for handling complex string formatting more efficiently.

Syntax:

“{}”.format(value)

value is nothing but the integer, float, character, string even a variable also.

Press enter or click to view image in full size

9. find():

String index() Method allows a user to find the index of the first occurrence of an existing substring inside a given string. if the character is not present in the string then it will return a ValueError.

Syntax:

string.index(value)

Press enter or click to view image in full size

10. isalnum():

String isalnum() method checks whether all the characters in a given string are either alphabet or numeric (alphanumeric) characters. if the substring is present in given string then it will return True, otherwise it will be returns false.

Syntax:

Get Vrushali Walekar’s stories in your inbox

Join Medium for free to get updates from this writer.

String.isalnum(value)

Press enter or click to view image in full size

Here, isalnum() method is aplied for numeric, character and alphanumeric character also.

11. isalpha():

String isalpha() method checks wheather all characters in given string are an alphabet. if the substring is present in given string then it will be return True, otherwise it will be False.

Syntax:

string.isalpha()

Press enter or click to view image in full size

12. isascii():

Return true if all character in string are ascii, otherwise it will be False.

Syntax:

string.isascii(value)

Press enter or click to view image in full size

13. isdecimal():

String isdecimal() function returns true if all characters in a string are decimal, else it returns False.

Syntax:

string.isdecimal()

Press enter or click to view image in full size

14.isdigit:

String isdigit() method returns “True” if all characters in the string are digits, Otherwise, It returns “False”.

Syntax:

string.isdigit()

Press enter or click to view image in full size

15. isidentifier():

String isidentifier() method is used to check whether a string is a valid identifier or not. The method returns True if the string is a valid identifier, else returns False.

Syntax:

string.identifier()

Press enter or click to view image in full size

17. islower():

String islower() method checks if all characters in the string are lowercase.This method returns True if all alphabets in a string are lowercase alphabets. If the string contains at least one uppercase alphabet, it returns False.

Press enter or click to view image in full size

18. isnumeric():

This method returns True if all alphabets in a string are lowercase alphabets. If the string contains at least one uppercase alphabet, it returns False.

Syntax:

string.isnumeric()

Press enter or click to view image in full size

19. isprintable():

String isprintable() is a built-in method used for string handling. The isprintable() method returns “True” if all characters in the string are printable or the string is empty, Otherwise, It returns “False”.

Syntax:

string.isprintable()

Press enter or click to view image in full size

20. isspace():

String isspace() method returns “True” if all characters in the string are whitespace characters, Otherwise, It returns “False”. This function is used to check if the argument contains all whitespace characters, such as:

  • ‘ ‘ — Space
  • ‘\t’ — Horizontal tab
  • ‘\n’ — Newline
  • ‘\v’ — Vertical tab
  • ‘\f’ — Feed
  • ‘\r’ — Carriage return

Syntax:

string.isspace()

Press enter or click to view image in full size

21. istitle():

String istitle() Method is a built-in string function that returns True if all the words in the string are title cased, otherwise returns False.

Syntax:

string.istitle()

Press enter or click to view image in full size

22. isupper():

String isupper() method returns whether all characters in a string are uppercase or not, True if all the letters in the string are in the upper case and False if even one of them is in the lower case.

Syntax:

string.isupper()

Press enter or click to view image in full size

Here, we see the some built-in function of string datatype in python. In next article we will see the few more built-in function.

#vevcodelab

http://vevcodelab.com/

--

--