Some Basic Python String Programs-4


Python program to check if the string is ASCII

string = "Beta Python: Python for U"

#using all() and ord() 
if all(ord(ch) < 128 for ch in string):
    print(f"{string} is ASCII string")
else:
    print(f"{string} is not an ASCII string")
#Output: Beta Python: Python for U is ASCII string

#using lambda and encode()
if lambda ch: len(ch) == len(ch.encode()):
    print(f"{string} is ASCII string")
else:
    print(f"{string} is not an ASCII string")
#Output: Beta Python: Python for U is ASCII string
  • The built-in ord() function returns the Unicode code point representation of the character passed.
  • The all() built-in method returns True If all the elements in the iterable passed are true else it returns False.
  • Python treats everything as an object and objects are always true in python. However, null values, None, 0, empty data types are observed as false in python.
  • The format() built-in string method returns a formatted representation of the string controlled by the format specifier. The replacement fields are expressions enclosed in curly brackets in a format string. They are evaluated at run time and then formatted.
  • The built-in encode() function returns the encoded version of the string according to the specified encoding.
  • The lambda function is a function that is defined without a name. It can have any number of arguments but only one expression as it is a small function having a single line of code. The expression is evaluated and returned.

Python program to convert a String to Binary

str1 = "Beta Python: Python for U"

#using ord()
str2 = ''.join(format(ord(ch), 'b') for ch in str1)
print(str2)
#Output: 10000101100101111010011000011000001010000111100111101001101000110111111011101110101000001010000111100111101001101000110111111011101000001100110110111111100101000001010101

#using bytearray()
str3 = ''.join(format(i, 'b') for ch in bytearray(str1, encoding = 'utf-8'))
print(str3)
#Output: 10000101100101111010011000011000001010000111100111101001101000110111111011101110101000001010000111100111101001101000110111111011101000001100110110111111100101000001010101
  • The built-in ord() function returns the Unicode code point representation of the passed character.
  • TheĀ  ‘b’ in format() function is used to represent the binary format of the string.
  • The join() built-in string method combines all the elements of iterables passed and returns a concatenated string.
  • The bytearray() built-in method returns a bytearray object which is a mutable sequence of integers in the range of 0 to 256.
  • The built-in len() function returns the number of elements present in a container.

Python program to convert hex string to decimal

hex_no = 'F'
dec_no = int(hex_no, 16)
print(dec_no)
#Output: 15
  • The built-in int() function returns an integer object from a number in the given base.

Python program to extract digits from the string

str1 = "aa2bb3cc4eee7"

#using list comprehension
str2 = ''.join([ch for ch in str1 if ch.isdigit()])
print(str2)
#Output: 2347

#using filter() and lambda 
str3 = ''.join(filter(lambda ch: ch.isdigit(), str1))
print(str3) 
#Output: 2347
  • The isdigit() built-in string method returns True if all characters in the string are digits, else, it returns False.
  • The filter() built-in method filters the given iterable with the help of a function that tests each element in the iterable to be True or False.

Python program to extract only characters from the string

str1 = "Beta Python: Python for U"

#using list comprehension 
str2 = ''.join([ch for ch in str1 if ch.isalpha()]) 
print(str2) 
#Output: BetaPythonPythonforU

#using filter() and lambda 
str3 = ''.join(filter(lambda ch: ch.isalpha(), str1)) 
print(str3) 
#Output: BetaPythonPythonforU
  • The isalpha() built-in string method returns True if all characters in the string are alphabets, else, it returns False.

Python program to extract all characters except letters and numbers

str1 = "@Beta1& #Python2:3 #Python2 for4 $U5 6 |"

#using list comprehension, isalpha() and isnumeric()
str2 = ''.join([ch for ch in str1 if (not ch.isalpha()) and (not ch.isnumeric())]) 
print(str2) 
#Output: @& #: # $ | 

#using filter(), isalpha() and isnumeric()
str3 = ''.join(filter(lambda ch: (not ch.isalpha()) and (not ch.isnumeric()), str1)) 
print(str3)
#Output: @& #: # $ | 

#using list comprehension and isalnum()
str4 = ''.join([ch for ch in str1 if not ch.isalnum()]) 
print(str4) 
#Output: @& #: #  $  |

#using filter(), lambda and isalnum()
str5 = ''.join(filter(lambda ch: not ch.isalnum(), str1)) 
print(str5) 
#Output: @& #: # $ |
  • The isnumeric() built-in method returns True if all the characters in a string are numeric characters, else, it returns False.
  • The isalnum() built-in method returns True if all the characters in the string are alphanumeric (either alphabets or numbers), else, it returns False.

Related Posts-

Leave a Comment

%d bloggers like this: