Some Basic Python String Programs-1


String

A string is an immutable sequence of  Unicode characters. Unicode includes every character in all languages. Python does not have a character data type, a single character is simply a substring with a length of 1 and can be accessed by using indexing. The string module contains several useful constants and classes, as well as many built-in methods. Strings in Python can be created by enclosing characters in single quotes or double quotes or even triple quotes.


Python Program to Merge Two or More Strings

  • The ‘+’ operator joins multiple strings together just like it adds multiple numbers together.
  • The join() built-in string method combines all the elements of iterables passed and returns a concatenated string.

Python Program to Concatenate Triple quote Strings

  • The splitlines() built-in string method splits the string at line breaks and returns a list of words of the string.
  • The zip() built-in method returns an iterator of tuples, grouping the similar index of multiple sequences so that they can be used as a single entity.
  • The strip() built-in string method removes any spaces present at the beginning and at the end of the string.

Python Program to compare strings

  • The is operator depicts if both the strings point to the same object.
  • The == operator checks whether both the strings are equal.

Python program to print a string with quotes

  • The backslash “\” is called an escape character in python. If a special character is prefixed with an escape character then it gets turned it into an ordinary string character.

Python Program to Print a String with a Backslash “\”

The backslash (“\”) character used as escape characters, directs the compiler to take a suitable action mapped to character prefixed with it.

  • A Python raw string treats backslash (‘\’) as a literal character. Without it, every backslash (‘\’) in a regular expression would have to be prefixed with another character to escape it. A string can be made raw string just by adding an ‘r’ or ‘R’ at the start of it.

Add Comment

%d bloggers like this: