Some Basic Python String Programs-2


Python Program to split a string into groups of n consecutive characters

  • List comprehension is a compact method that defines a list and its contents at the same time. Its basic syntax consists of a square bracket with an expression that is executed for each element along with for loop to iterate over each element. Read more: List Comprehension in Python – Explained with Examples

Python Program to check if a string is a subset of another string

  • 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 prefix f before a string tells Python that the string is a format string. The replacement fields are expressions enclosed in curly brackets in a format string. They are evaluated at run time and then formatted.
  • The backslash (“\”) character used as escape characters, directs the compiler to take a suitable action mapped to character prefixed with it.
  • The issubset() built-in method returns True if all the elements of a set are present in the iterable passed else it returns False.

Python Program to remove multiple empty spaces from a string List

  • The strip() built-in string method removes any spaces present at the beginning and the end of the string.

Python program to count words in a sentence

  • The split() built-in method breaks up a string by the specified separator and returns a list of strings. If a separator is not specified then white space is considered as a separator.
  • The len() built-in function returns the number of elements present in a container.

Python program to add leading Zeros to a string

  • The rjust() built-in string method returns a new string of given length after filling the remaining space on the left side of the original string with the character specified.
  • The zfill() built-in string method returns a copy of the string with ‘0’ characters padded to the left side of the original string.

Python program to add trailing Zeros to a string

  • The ljust() built-in string method returns a new string of given length after filling the remaining space on the right side of the original string with the character specified.
  • The operator ‘<‘  is used to left-align the string and fill the remaining space of the length given with the character specified.

Python program to Right and Left Shift characters in a String

  • The ‘*’ is used to repeat the string for a given number of times when used with a string type.
  • Slicing extracts a subset of elements from a sequence. It mainly uses three parameters strat, stop, and step enclosed in square brackets []. Read more: Slicing in python
  • The ‘%’ operator(modulus operator) returns the remainder of the left operand by the right.
  • The ‘+’ operator concatenates multiple strings to a single string.

Related Posts-

Add Comment

%d bloggers like this: