Some Basic Python String Programs-4


Python program to check if the string is ASCII

  • 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

  • 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

  • The built-in int() function returns an integer object from a number in the given base.

Python program to extract digits from the string

  • 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

  • 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

  • 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-

Add Comment

%d bloggers like this: