Min menu

Pages

Compare functions in the str class for text handling in Python

Compare functions in the str class to work with text in Python 

Python  functionstartswith()

its definition

Used to find out if the invoked text starts with a specific text or not.
The location of the parameter subcan be passed plain text, or an array of texts of its type tuple.

  • If you pass a plaintext in place of the parameter sub, it returns Trueif its characters match the characters at the beginning of the text that called it. Otherwise it returns False.

  • If you pass an array of texts of type in tupleplace of the parameter sub, it returns Trueif there is a text whose characters match the text that started with the text that called it. Otherwise it returns False.



built

                  startswith(sub[, start[, end]])
	  


parameters

  • subIt is a plain text or an array of texts type tuple.

  • startAn optional parameter, you can pass in its place an integer specifying the number of the cell from which you want to start the search.

  • endAn optional parameter, you can pass in its place an integer specifying the number of the cell before which you want to stop the search.

Note: start You endcan pass the values ​​in their place only if you pass plain text in place of the parameter sub.



Return value

If the text that called it starts with the parameter textsub (or the text of one of its elements if it is an array)Truecome back
In the event that the parameter text substarts at the cell that we specified the location of the parameter startin the text that called it, it is returned True.
If an empty text is passed in place of the parameter, it subis returned True.
Otherwise it returns False.



first example

Test.py
                    s = 'welcome to alawiri.com'; # s Here we have defined a text variable named
print(s.startswith('welcome to')) #s is at the beginning of the text of the variable 'welcome to' because the text True will return startswith() here the function print(s.startswith('welc')) #s is at the beginning of the text of the variable 'welc' because the text True will return startswith() here the function print(s.startswith('alawiri')) #s does not exist at the beginning of the text of the variable 'alawiri' because the text False will return startswith() here the function
print(s.startswith('alawiri', 11)) #s is at the beginning of the cell Number 11 in the variable 'alawiri' because the text True will return startswith() here the function
print(s.startswith('')) # Because we are testing the value of the empty text True will return startswith() here the function

We will get the following result when running.

True
True
False
True
True


second example

Test.py
                    # s Here we have defined a text variable named
		  s = 'Python is a programming language.'
	  
		  # It contains 3 tuples Here we have defined an array of text values
		  arr = ('Java', 'Python', 'PHP')
	  
		  # True 'PHP' or 'Python' or 'Java' text will be printed if it starts with .s text here we tested the value of the variable
		  print(s.startswith(arr))
	

We will get the following result when running.

True

Python  function endswith()

its definition

Used to find out if the invoked text ends with a specific text or not.
The location of the parameter suffixcan be passed plain text, or an array of texts of its type tuple.

  • If you pass a plaintext in place of the parameter suffix, it returns Trueif its characters match the characters at the end of the text that called it. Otherwise it returns False.

  • If you pass an array of strings of type in tupleplace of the parameter suffix, it returns Trueif there is a string whose characters match the string ending in the invoked string. Otherwise it returns False.



built

                  endswith(suffix[, start[, end]])
	  


parameters

  • suffixIt is a plain text or an array of texts type tuple.

  • startAn optional parameter, you can pass in its place an integer specifying the number of the cell at which you want to stop the search.

  • endAn optional parameter, you can pass in its place an integer specifying the number of the cell before which you want to start the search.

Note: start You endcan pass the values ​​in their place only if you pass plain text in place of the parameter suffix.



Return value

If the text that called it ends with the parameter textsuffix (or the text of one of its elements if it is an array)Truecome back
In the event that the parameter text suffixends at the cell that we specified the location of the parameter startin the text that called it, it returns True.
If an empty text is passed in place of the parameter, it suffixis returned True.
Otherwise it returns False.



first example

Test.py
                    s = 'welcome to alawiri.com'; # s Here we have defined a text variable named
	  
		  print(s.endswith('alawirs.com')) # s is at the end of the variable 'welcome to' because text True will return endswith() here function
		  print(s.endswith('alawiri')) # s does not exist at the end of the variable 'alawiri' because text False will return endswith() here 
print(s.endswith('alawiri', 11)) # s does not exist at the beginning Place #11 in 'alawiri' because False will return endswith() Here the function
print(s.endswith('alawiri', 11, 18)) # s is at the beginning of block 11 and before field 18 in the variable 'alawiri' Because text True returns endswith() here function
print(s.endswith('')) # Because we are testing for null text True returns endswith() here function

We will get the following result when running.

True
True
False
True
True


second example

Test.py
                    # s Here we have defined a text variable named
		  s = 'My favorite language is Python'
	  
		  # It contains 3 tuples Here we have defined an array of text values
		  arr = ('Java', 'Python', 'PHP')
	  
		  # True 'PHP' or 'Python' or 'Java' text will be printed if it ends with text .s Here we tested the value of the variable
		  print(s.endswith(arr))
	

We will get the following result when running.

True

Python  functionisalpha()

its definition

Used to find out if the text that called it contains only an alphanumeric character or an alphanumeric character set.



built

                  str.isalpha()
	  


parameters

Do not accept any parameters.



Return value

Returns Trueif the script that called it contains only an alphanumeric character or an alphanumeric character set.
Otherwise it returns False.



Example

Test.py
                    s1 = 'a' # We put an alphabetic character s1 in it. Here we have defined a text variable named
		  s2 = 'abc' # we put in it an alphanumeric character set s2 here we have defined a text variable named
		  s3 = 'a c' # We put two characters in it with a blank space between them s3 Here we have defined a text variable named
		  s4 = '5' # We put the number s4 in it here we have defined a text variable named
		  s5 = 'A+' # + we put an alphabetic character in it and the symbol s5 here we have defined a text variable named
		  s6 = '' # We didn't put anything in it s6 Here we have defined a text variable named
	  
		  print(s1.isalpha()) # True which will return isalpha() here we have printed what the function will return
		  print(s2.isalpha()) # True which will return isalpha() here we have printed what the function will return
		  print(s3.isalpha()) # False which will return isalpha() here we have printed what the function will return
		  print(s4.isalpha()) # False which will return isalpha() here we have printed what the function will return
		  print(s5.isalpha()) # False which will return isalpha() here we have printed what the function will return
		  print(s6.isalpha()) # False which will return isalpha() here we have printed what the function will return
	

We will get the following result when running.

True
True
False
False
False
False

Python  functionisnumeric()

its definition

Used to find out if the text that called it contains only a number or a group of numbers or not.



built

                  str.isnumeric()
	  


parameters

Do not accept any parameters.



Return value

Returns Trueif the script that called it contains only a number or group of numbers.
Otherwise it returns False.



Example

Test.py
                    s1 = '5' # We put the number s1 in it here we have defined a text variable named
		  s2 = '500' # We put s2 numbers in it. Here we have defined a text variable named
		  s3 = '½' # We put in it a symbol representing 1 divided by 2 s3 Here we have defined a text variable named
		  s4 = '5 0' # We put two numbers in it with a blank space between them s4 Here we have defined a text variable named
		  s5 = '3D' # We put a letter and number in it s5 Here we have defined a text variable named
		  s6 = '' # We didn't put anything in it s6 Here we have defined a text variable named
	  
		  print(s1.isnumeric()) # True which will return isnumeric() here we have printed what the function will return
		  print(s2.isnumeric()) # True which will return isnumeric() here we have printed what the function will return
		  print(s3.isnumeric()) # True which will return isnumeric() here we have printed what the function will return
		  print(s4.isnumeric()) # False which will return isnumeric() here we have printed what the function will return
		  print(s5.isnumeric()) # False which will return isnumeric() here we have printed what the function will return
		  print(s6.isnumeric()) # False which will return isnumeric() here we have printed what the function will return
	

We will get the following result when running.

True
True
True
False
False
False

Python  functionisdigit()

its definition

Used to find out if the text that called it contains only a number or a group of numbers or not.
Note: This function does not consider symbols that represent numeric values ​​such as the ½ symbol as a number.



built

                  str.isdigit()
	  


parameters

Do not accept any parameters.



Return value

Returns Trueif the script that called it contains only a number or group of numbers.
Otherwise it returns False.



Example

Test.py
                    s1 = '5' # We put the number s1 in it here we have defined a text variable named
		  s2 = '500' # We put s2 numbers in it. Here we have defined a text variable named
		  s3 = '½' # We put in it a symbol representing 1 divided by 2 s3 Here we have defined a text variable named
		  s4 = '5 0' # We put two numbers in it with a blank space between them s4 Here we have defined a text variable named
		  s5 = '3D' # We put a letter and number in it s5 Here we have defined a text variable named
		  s6 = '' # We didn't put anything in it s6 Here we have defined a text variable named
	  
		  print(s1.isdigit()) # True which will return isdigit() here we have printed what the function will return
		  print(s2.isdigit()) # True which will return isdigit() here we have printed what the function will return
		  print(s3.isdigit()) # False which will return isdigit() here we have printed what the function will return
		  print(s4.isdigit()) # False which will return isdigit() here we have printed what the function will return
		  print(s5.isdigit()) # False which will return isdigit() here we have printed what the function will return
		  print(s6.isdigit()) # False which will return isdigit() here we have printed what the function will return
	

We will get the following result when running.

True
True
False
False
False
False

Python  functionisalnum()

its definition

Used to find out what type of text characters it called.



built

                  str.isalnum()
	  


parameters

Do not accept any parameters.



Return value

  • Returns Trueif the script that called it contains only alphanumeric characters.

  • Returns Trueif the invoking script contains only alphanumeric characters.

  • Returns Trueif the script that called it contains only numbers.

  • Returns Trueif the text that called it contains only one letter or number.

  • Otherwise it returns False.



Example

Test.py
                    s1 = 'Python' # we put the s1 alphabets in it here we have defined a text variable named
		  s2 = '123456' # We put s2 numbers in it here we have defined a text variable named
		  s3 = '3D' # We put a letter and number in it s3 Here we have defined a text variable named
		  s4 = "90s" # we put the alphabet and number s4 in it, here we have defined a text variable named
		  s5 = 'A' # We put an alphabetic character in it s5 Here we have defined a text variable named
		  s6 = '6' # We put the number s6 in it here we have defined a text variable named
		  s7 = '' # We didn't put anything in it s7 Here we have defined a text variable named
		  s8 = 'facebook.com' # we put alphanumeric characters and dot s8 here we have defined a text variable named
		  s9 = 'facebook and google' # we put alphanumeric characters and empty spaces in it s9 here we have defined a text variable named
	  
		  print(s1.isalnum()) # True which will return isalnum() here we have printed what the function will return
		  print(s2.isalnum()) # True which will return isalnum() here we have printed what the function will return
		  print(s3.isalnum()) # True which will return isalnum() here we have printed what the function will return
		  print(s4.isalnum()) # True which will return isalnum() here we have printed what the function will return
		  print(s5.isalnum()) # True which will return isalnum() here we have printed what the function will return
		  print(s6.isalnum()) # True which will return isalnum() here we have printed what the function will return
		  print(s7.isalnum()) # False which will return isalnum() here we have printed what the function will return
		  print(s8.isalnum()) # False which will return isalnum() here we have printed what the function will return
		  print(s9.isalnum()) # False which will return isalnum() here we have printed what the function will return
	

We will get the following result when running.

True
True
True
True
True
True
False
False
False

Python  functionislower()

its definition

Used to see if the text that called it contains only lowercase letters(Small Letters)only or not.
Return Trueif it is, otherwise return False.

Note: The presence of blank spaces within the text content does not affect the return result.



built

                  str.islower()
	  


parameters

Do not accept any parameters.



Return value

Returns Trueif the script that called it contains only lowercase letters.
Otherwise it returns False.



Example

Test.py
                    s1 = 'python language' # we put a text consisting of only lowercase letters s1 here we have defined a text variable named
		  s2 = 'Python Language' # we put a text consisting of only lowercase letters s1 here we have defined a text variable named
	  
		  print(s1.islower()) # True which will return islower() here we have printed what the function will return
		  print(s2.islower()) # False which will return islower() here we have printed what the function will return
	

We will get the following result when running.

True
False

Python  functionisupper()

Defining the isupper() function in Python

Used to see if the text that called it contains only uppercase letters(Capital Letters)only or not.
Return Trueif it is, otherwise return False.

Note: The presence of blank spaces within the text content does not affect the return result.



Build the isupper() function in Python

                  str.isupper()
	  


Parameters of the isupper() function in Python

Do not accept any parameters.



Return value of the isupper() function in Python

Returns Trueif the script that called it contains only uppercase letters.
Otherwise it returns False.



Example

Test.py
                    s1 = 'PYTHON LANGUAGE' # We put in it a text consisting of only lowercase letters s1 Here we have defined a text variable named
		  s2 = 'Python Language' # we put a text consisting of only lowercase letters s1 here we have defined a text variable named
	  
		  print(s1.isupper()) # True which will return isupper() here we have printed what the function will return
		  print(s2.isupper()) # False which will return isupper() here we have printed what the function will return
	

We will get the following result when running.

True
False

Python  functionistitle()

Defining the istitle() function in Python

Used to find out if the first letter of each word in the text that recalled it is a capital letter(Capital Letter)or not.
come backTrue if it is, otherwise return False.



Build the istitle() function in Python

                  str.istitle()
	  


Parameters of the istitle() function in Python

Do not accept any parameters.



Return value of the istitle() function in Python

come backTrue if the first letter of each word in the text that called it was a capital letter(Capital Letter).
Otherwise it returns False.



Example

Test.py
                    s1 = 'Learn Python For Beginners' # We put in it a text containing a group of words each beginning with a capital letter s1 Here we have defined a text variable named
		  s2 = 'Learn python for beginners' # We put in it a text that contains a set of words, only the first one of them starts with a capital letter s2. Here we have defined a text variable named
	  
		  print(s1.istitle()) # True which will return istitle() here we have printed what the function will return
		  print(s2.istitle()) # False which will return istitle() here we have printed what the function will return
	

We will get the following result when running.

True
True
False
False

Python  function isspace()

Defining the isspace() function in Python

Used to see if the invoked text contains a blank space(White Space)Or just several blank spaces or not.
Return Trueif it is, otherwise return False.



Build the isspace() function in Python

                  str.isspace()
	  


Parameters of the isspace() function in Python

Do not accept any parameters.



Return value of the isspace() function in Python

Returns Trueif the text that called it contains only a blank space(White Space)Or just several blank spaces.
Otherwise it returns False.



Example

Test.py
                    s1 = ' ' # we put a text containing an empty space s1 here we have defined a text variable named
		  s2 = ' '# We put 4 blank spaces in it s2 Here we have defined a text variable named
		  s3 = 'Hi ' # We put a text in it consisting of alpha characters and blank spaces s3 Here we have defined a text variable named
		  s4 = '' # We didn't put anything in it s4 Here we have defined a text variable named
	  
		  print(s1.isspace()) # True which will return isspace() here we have printed what the function will return
		  print(s2.isspace()) # True which will return isspace() here we have printed what the function will return
		  print(s3.isspace()) # False which will return isspace() here we have printed what the function will return
		  print(s4.isspace()) # False which will return isspace() here we have printed what the function will return
	

We will get the following result when running.

True
True
False
False