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.
#using single quotes str1 = 'Python' print(str1) print(type(str1)) #Output: Python <class 'str'> #using double quotes str2 = "Python" print(str2) #Output: Python #using triple quotes str3 = '''Python''' print(str3) #Output: Python #triple are commomnly used for creating multiline strings str4 = """Beta Python !!!Python For You!!!!""" print(str4) #Output: Beta Python !!!Python For You!!! #using str()constructer method num = 2376 str5 = str(num) print("num:",num,"Type of num:",type(num)) print("string:",str5,"Type of string:",type(str5)) #Output: num: 2376 Type of num: <class 'int'> string: 2376 Type of string: <class 'str'>
Python Program to Merge Two or More Strings
str1 = "Beta Python:" str2 = "Python For You" #using + operator str3 = str1 + str2 str4 = str1 + " " + str2 print("Concatenate strings without space:",str3) print("Concatenate strings with space:",str3) #Output: Concatenate strings without space: Beta Python:Python For You Concatenate strings with space: Beta Python: Python For You #using join() method str5 = "".join([str1, str2]) str6 = " ".join([str1, str2]) print("Concatenate strings without space:",str5) print("Concatenate strings with space:",str6) #Output: Concatenate strings without space: Beta Python:Python For You Concatenate strings with space: Beta Python: Python For You
- 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
str1 = """Beta Python""" str2 = '''Python: For You''' list_str1 = str1.splitlines() list_str2 = str2.splitlines() con_str = [] for i, j in zip(test_str1, list_str2): con_str.append(" " + i.strip() + " " + j.strip()) con_str = '\n'.join(con_str) print(con_str) #Output: Beta Python: Python For You
- 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
str1 = "Python" str2 = "Python" print(str1 is str2) #Output: True print(str1 == str2) #Output: True str3 = ''.join(['Py', 'th', 'on']) str4 = ''.join(['Pyt', 'hon']) print(str3 is str4) #Output: False print(str3 == str4) #Output: True
- 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
#Using triple qoutes str1 = """"Python" For You""" print(str1) #Output: "Python" For You str2 = ''''Python' For You''' print(str2) #Output: 'Python' For You str3 = """'Python' For You""" print(str3) #Output: 'Python' For You" str4 = '''"Python" For You''' print(str4) #Output: "Python" For You #using backslash ("\") str5 = "\"Python\" For You" print(str5) #Output: "Python" For You str6 = "\'Python\' For You" print(str6) #Output: 'Python' For You
- 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.
#using \\ str1 = '''"\\" is a backslash''' print(str1) #Output: "\\" is a backslash #using raw string str2 = r'''"\" is a backslash''' print(str2) #Output: "\\" is a backslash
- 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.