Change language

Python String isalpha () and its application

| |

In Python, isalpha () — it is a built-in method used to process strings. 
The isalpha () methods return True if all characters in the string are alphabets. Otherwise, it returns "False". 
This function is used to check if the argument contains alphabet characters such as:

 ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz 

Syntax :

  string.isalpha ()   Parameters:  isalpha () does not take any parameters  Returns:  1.True- If all characters in the string are alphabet ... 2.False- If the string contains 1 or more non-alphabets. 

Examples:

 Input: string = ’Ayush’ Output: True Input: string =’ Ayush Saxena’ Output: False Input: string = ’Ayush0212’ Output: False 

# Python code to implement isalpha ()

 
# checking alphabets

string = ’Ayush’

print (string.isalpha ())

 

 

string = ’Ayush0212’

print (string.isalpha ())

  
# check if space is alphabetical

string = ’Ayush Saxena’

print (string.isalpha ())

Output:

 True False False 

Errors and exceptions

  1. It contains no arguments, so an error occurs when passing the parameter
  2. Both uppercase and lowercase alphabets return "True"
  3. Space is not considered an alphabet, so it returns "False"

Usage: If given a string in python, count the number of alphabets on a line and print the alphabets. 
Examples:

 Input: string = ’Ayush Saxena’ Output: 11 AyushSaxena Input: string =’ Ayush0212’ Output: 5 Ayush 

Algorithm

1. Initialize a new line and variable count to 0.
2. Go through the given string character by character to its length, check if the character is an alphabet. 
3. If it’s an alphabet, increment the counter by 1 and add it on a new line, otherwise skip to the next character. 
4. Print the counter value and a new line.

# Python program for illustration
# counting the number of alphabets
# using isalpha ()

  
# Specified string

string = ’Ayush Saxena’

count = 0

 
# Initialize new lines

newstring1 = ""

newstring2 = ""

  
# Iterate a string and check alphabets
# Increment counter if alphabet found
# Finally, print invoice

for a in string:

if (a.isalpha ()) = = True :

count + = 1

newstring1 + = a

pr int (count)

print (newstring1 )

 
# This line

string = ’Ayush0212’

count = 0

for a in string:

if (a.isalpha ()) = = True :

count + = 1

  newstring2 + = a

print (count)

print (newstring2)

Output:

 11 AyushSaxena 5 Ayush 

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically