BSR Tutorials

Python - Special characters in string

A string is a sequence of charachters. The string can contain special characters like \n, \t, \r, \b, \f, \', \'', \\, \v and more.
You can go through the Special Characters tutorial to know more about them.


Program Code



    # Special Characters Example

    # String with charcters
    print("I'm Learning Python!")
    
    # String with special character \n
    print("I'm Learning\nPython")
    
    # String with special character \t
    print("I'm Learning\tPython")
    
    # String with special character \r
    print("I'm Learning\rPython")
    
    # String with special character \b
    print("I'm Learning\bPython")
    
    # String with special character \f
    print("I'm Learning\fPython")
    
    # String with special character \"
    print("I'm Learning \"Python\"")
    
    # String with special character \'
    print("I'm Learning \'Python\'")

Output



    I'm Learning Python!

    I'm Learning
    Python

    I'm Learning    Python

    Pythonarning

    I'm LearninPython

    I'm Learning
                Python

    I'm Learning "Python"

    I'm Learning 'Python'

In this program, the print() funcation prints the string value which in double quotes("") or single quotes('').
When we added \n in string, it returns to the line.
When we added \t in string, it represents as a tab and it moves the tab space.
Like the same way, the special characters can be used in string in Python programming based on our need.

Python Examples


Example #1: Python - Dispaly Hello World (current program)