Category Python

isalpha(): checking letters only in a string in Python

how to python

Use the isalpha() method to check if a string only contains letters. word = 'beach' print(word.isalpha()) #output: True word = '32' print(word.isalpha()) #output: False word = 'number32' print(word.isalpha()) #output: False word = 'Favorite number is blue' #notice the space between…

How to check leap year in Python

how to python

The rules to check a leap year are: The year has to be divisible by four, that is, the remainder of the division is 0. The year cannot be divisible by 100, that is, the remainder of the division is…

How to check if a number is prime in Python

how to python

A prime number is an integer greater than one that is only divisible by one and itself. def check_prime(number): if number <= 1: return False for divisor in range(2, int(number**0.5)+1): if (number % divisor) == 0: print(divisor,'*', number//divisor, '=', number)…

isdigit(): checking digits only in a string in Python

how to python

Use the isdigit() method to check if a string only contains digits. Digits include numbers from 0 to 9 and also superscripts and subscripts. word = '32' print(word.isdigit()) #output: True print("\u2083".isdigit()) #unicode for subscript 3 #output: True word = 'beach'…

isnumeric(): checking numerics only in a string in Python

how to python

Use the isnumeric() method to check if a string only contains numeric chars. Numerics include numbers from 0 to 9 and combinations of them, roman numerals, superscripts, subscripts, fractions, and other variations. word = '32' print(word.isnumeric()) #output: True print("\u2083".isnumeric()) #unicode…

How to swap two variables in Python

how to python

In this post I will show how to swap the values of two variables. The first way is very common in many languages and require the use of a third variable. The second and recommended way is what they call…