DEV Community

Cover image for Another 5 Helpful Python String Methods
Aya Bouchiha
Aya Bouchiha

Posted on

3 2

Another 5 Helpful Python String Methods

Hello, I'm Aya Bouchiha,

this is the part 2 of 5 useful python string methods.

capitalize()

capitalize(): this string method converts to uppercase the first letter of the first word in the specified string.

first_name = "aya"

print(first_name.capitalize()) # Aya
Enter fullscreen mode Exit fullscreen mode

isalpha()

isalpha(): checks if all string's characters are alphabets letters [A-z].

print('AyaBouchiha'.isalpha())   # True
print('Aya Bouchiha'.isalpha())  # False
print('Aya-Bouchiha'.isalpha())  # False
print('AyaBouchiha 1'.isalpha()) # False
print('Aya Bouchiha!'.isalpha()) # False
Enter fullscreen mode Exit fullscreen mode

isdigit()

isdigit(): checks if all string's characters are digits.

print('100'.isdigit())     # True
print('12a'.isdigit())     # False
print('100 000'.isdigit()) # False
print('+212-6000-00000'.isdigit()) # False
print('12a'.isdigit()) # False

print('3\u00B2') # 3²
print('3\u00B2'.isdigit()) # True
Enter fullscreen mode Exit fullscreen mode

isalnum()

isalnum(): checks if all string's characters are alphanumeric (alphabets, numbers).

print('2021'.isalnum()) # True
print('30kviews'.isalnum()) # True
print('+212600000000'.isalnum()) # False           
print('dev.to'.isalnum()) # False  
print('developer.aya.b@gmail.com'.isalnum()) # False   
print('Aya Bouchiha'.isalnum()) # False  
Enter fullscreen mode Exit fullscreen mode

strip()

strip(characters(optional)): lets you delete the given characters (by default => whitespaces) at the start and at the end of the specified string.

print(' Aya Bouchiha  '.strip()) # Aya Bouchiha
print('   +212612345678  '.strip(' +')) # 212612345678
print('Hi, I\'m Aya Bouchiha'.strip('Hi, ')) # I'm Aya Bouchiha
Enter fullscreen mode Exit fullscreen mode

Summary

  • capitalize(): converts to uppercase the first letter of the first word in the specified string.
  • isalpha(): checks if all string's characters are alphabets letters.
  • isdigit(): checks if all string's characters are digits.
  • isalnum(): checks if all string's characters are alphanumeric.
  • strip(): deletes the given characters (by default => whitespaces) at the start and at the end of the specified string.

Suggested Posts

To Contact Me:

email: developer.aya.b@gmail.com

telegram: Aya Bouchiha

Have a great day!

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

Top comments (0)

AWS GenAI LIVE image

How is generative AI increasing efficiency?

Join AWS GenAI LIVE! to find out how gen AI is reshaping productivity, streamlining processes, and driving innovation.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay