Python String Methods | Set 2 (length, number, center, lust, ray, isalfa, isalum, isspace and conjunction)



Other methods are discussed in this article

1. len () : — This function returns the length of the string.

2. count (“string”, beg, end) : — this function counts occurrences of the specified substring in the entire string. This function takes 3 arguments: ubstring, start position (default 0) and end position (default line length).

# Python code to demonstrate how it works
# len () and count ()

str = "pythonengineering is for geeks"

 
# Print line length using len ()

print ( "The length of string is:" , len ( str )); 

 
# Print the geeks entry on line
# Prints 2 because it only checks up to the 15th element

print ( "Number of appearance of" "geeks" "is:" , end = " ")

print ( str . count ( "geeks" , 0 , 15 ))

Output:

 The length of string is: 26 Number of appearance of geeks is: 2 

3. center () : — This function is used to surround a string with a character repeated on both sides of the string several times. By default, the character is a space. Takes 2 arguments, string length and character.

4. ljust () : — This function returns the original string shifted to the left , with a character to the right . He corrects the line on the left. By default, the character is a space. It also takes two arguments: string length and character. 

5. rjust () : — This function returns a right-shifted original string with a character to the left . This adjusts the line correctly. By default, the character is a space. It also takes two arguments: line length and character.

# Python code to demonstrate how it works
# center (), ljust () and rjust ()

str = " pythonengineering "

  
# Print line after centering with & # 39; - & # 39;

print ( " The string after centering with `-` is:" , end = "")

print ( str . center ( 20 , `-` ))

  
# Print line after ljust ()

print ( " The string after ljust is: " , end = "")

print ( str . ljust ( 20 , `-` ))

  
# Print line after rjust ()

print ( "The string after rjust is:" , end = " ")

print ( str . rjust ( 20 , `-` ))

Output:

 The string after centering with `-` is: --- pythonengineering ---- The string after ljust is: pythonengineering ------- The string after rjust is: ------- pythonengineering  

6. isalpha () : — This function returns true when all characters in the string are alphabets, otherwise it returns false.

7. isalnum () : — this function returns true if all characters in the string are combinations of numbers and / or alphabets; otherwise it returns false.

8. isspace () : — This function returns true when all characters in the string are spaces, otherwise it returns false.

# Python code for demonstration work
# isalpha (), isalnum (), isspace ()

str = "pythonengineering"

str1 = "123"

 
# Check if str contains all alphabets

if ( str . isalpha ()):

  print ( " All cha racters are alphabets in str " )

else : print ( "All characters are not alphabets in str" )

 
# Check whether all numbers are in str1

if (str1.isalnum ()):

print ( "All characters are numbers in str1" )

else : print ( "All characters are not numbers in str1 " )

  
# Check if all spaces are in str1

if (str1.isspace ()) :

print ( "All characters are spaces in str1" )

else : print ( "All characters are not spaces in str1" )

Output:

 All characters are alphabets in str All characters are numbers in str1 All characters are not spaces in str1 

9. join () : — This function is used to concatenate a sequence of lines mentioned in its arguments with a string.

# Python code to demonstrate how it works
# join ()

str = "_"

str1 = ( "geeks" , " for " , " geeks " )

 
# using join () to join str1 sequence to str

print ( "The string after joining is: " , end = " ")

print ( str . join (str1))

Exit :

 The string after joining is: geeks_for_geeks 

This article is contributing to Manjeet Singh If you love Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting the article [email protected] See my article appearing on the Python.Engineering homepage and help other geeks.

Please post comments if you find anything wrong or if you would like to share more information on the topic discussed above.