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

isspace | Python Methods and Functions | String Variables

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.





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

Answer #1

I once wrote something similar to Bartek"s answer and javascript inspired:

def is_not_blank(s):
    return bool(s and not s.isspace())

Test:

print is_not_blank("")    # False
print is_not_blank("   ") # False
print is_not_blank("ok")  # True
print is_not_blank(None)  # False

Answer #2

Use the str.isspace() method:

Return True if there are only whitespace characters in the string and there is at least one character, False otherwise.

A character is whitespace if in the Unicode character database (see unicodedata), either its general category is Zs (“Separator, space”), or its bidirectional class is one of WS, B, or S.

Combine that with a special case for handling the empty string.

Alternatively, you could use str.strip() and check if the result is empty.

Answer #3

Why not use title Right from the docs:

>>> "they"re bill"s friends from the UK".title()
"They"Re Bill"S Friends From The Uk"

If you really wanted PascalCase you can use this:

>>> "".join(x for x in "make IT pascal CaSe".title() if not x.isspace())
"MakeItPascalCase"