Check Type of Characters Present in a String
Python contains the following methods to check type of characters present in a String.
1. isalnum()
Returns True if all characters are alphanumeric (a to z , A to Z ,0 to9)
2. isalpha()
Returns True if all characters are only alphabet symbols (a to z,A to Z)
3. isdigit()
Returns True if all characters are digits only( 0 to 9)
4. islower()
Returns True if all characters are lower case alphabet symbols
5. isupper()
Returns True if all characters are upper case alphabet symbols
6. istitle()
Returns True if string is in title case
7. isspace()
Returns True if string contains only spaces
E.g
print('Ashok123'.isalnum()) ==> True print('Ashok123'.isalpha()) ==> False print('Ashok'.isalpha()) ==> True print('Ashok'.isdigit()) ==> False print('786786'.isdigit()) ==> True print('abc'.islower()) ==> True print('Abc'.islower()) ==> False 8 print('abc123'.islower()) ==> True 9 print('ABC'.isupper()) ==>True 1 print('Learning python is Easy'.istitle()) ==> False 1 print('Learning Python Is Easy'.istitle()) ==> True 12 print(' '.isspace()) ==> True
Formatting the Strings
We can format the strings with variable values by using replacement operator {} and format() method. The main objective of format() method to format string into meaningful output form.
1. Basic formatting for default, positional and keyword arguments.
name = 'Ashok' salary = 60000 age = 28 print("{} 's salary is {} and his age is {}".format(name,salary,age)) print("{0} 's salary is {1} and his age is {2}".format(name,salary,age)) print("{x} 's salary is {y} and his age is {z}".format(z=age,y=salary,x=name))
Output
Ashok 's salary is 60000 and his age is 28 Ashok 's salary is 60000 and his age is 28 Ashok 's salary is 60000 and his age is 28
2. Formatting Numbers
d ==> Decimal IntEger
f ==> Fixed point number(float).The default precision is 6
b ==> Binary format
o ==> Octal Format
x ==> Hexa Decimal Format (Lower case)
X ==> Hexa Decimal Format (Upper case)
print("The integer number is: {}".format(123)) print("The integer number is: {:d}".format(123)) print("The integer number is: {:5d}".format(123)) print("The integer number is: {:05d}".format(123))
Output
The integer number is: 123 The integer number is: 123 The integer number is: 123 The integer number is: 00123
E.g 2
print("The float number is: {}".format(123.4567)) print("The float number is: {:f}".format(123.4567)) print("The float number is: {:8.3f}".format(123.4567)) print("The float number is: {:08.3f}".format(123.4567)) print("The float number is: {:08.3f}".format(123.45)) print("The float number is: {:08.3f}".format(786786123.45))
Output
The float number is: 123.4567 The float number is: 123.456700 The float number is: 123.457 The float number is: 0123.457 The float number is: 0123.450 The float number is: 786786123.450
Note
{:08.3f} means
- Total positions should be minimum 8.
- After decimal point exactly 3 digits are allowed. If it is less then 0s will be placed in the last positions
- If total number is < 8 positions then 0 will be placed in MSBs
- If total number is >8 positions then all integral digits will be considered.
- The extra digits we can take only 0
E.g 3
print("Binary Form:{0:b}".format(153)) print("Octal Form:{0:o}".format(153)) print("Hexa decimal Form:{0:x}".format(154)) print("Hexa decimal Form:{0:X}".format(154))
Output
Binary Form:10011001 Octal Form:231 Hexa decimal Form:9a Hexa decimal Form:9A
Note
We can represent only int values in binary, octal and hexadecimal and it is not possible for float values.
3. Number formatting for signed numbers
- While displaying positive numbers,if we want to include + then we have to write {:+d} and {:+f}
- Using plus for -ve numbers there is no use and for -ve numbers – sign will come automatically.
print("int value with sign:{:+d}".format(123)) print("int value with sign:{:+d}".format(-123)) print("float value with sign:{:+f}".format(123.456)) print("float value with sign:{:+f}".format(-123.456))
Output
int value with sign:+123 int value with sign:-123 float value with sign:+123.456000 float value with sign:-123.456000
4. Formatting Date values
import datetime #datetime formatting date=datetime.datetime.now() print("It's now:{:%d/%m/%Y %H:%M:%S}".format(date))
Output
It's now:09/09/2020 12:36:26
5. Formatting complex numbers
complexNumber=1+2j print("Real Part:{0.real} and Imaginary Part:{0.imag}".format(complexNumber))
Output
Real Part: 1.0 and Imaginary Part: 2.0
6. Formatting class members using format()
class Person: age=29 name="Ashok Kumar" print("{p.name}'s age is :{p.age}".format(p=Person()))
Output
Ashok Kumar's age is :29