Strings in Python

Strings are immutable. They can be expressed in several ways in single quotes or double quotes:

>>> ’Hello World’ ’Hello World’ # using single quote

>>> “doesn’t” “doesn’t” # using double quotes

>>> ’”Yes,” he said.’ # A mix of both ’”Yes,” he said.’

>>> “\”Yes,\” he said.” ’”Yes,” he said.’

>>> u’Hello World’ # Creates a unicode string u’Hello World’

>>> “””Hello World””” # Using triple quote ‘Hello World’

>>> ”’Hello World”’ ‘Hello World’

The small ‘u’ in front of the quote indicates that a Unicode string is to be created


>>> r’Hello \n World’             # Creates a raw string that do

# not give special attention to

# escape sequences.

‘Hello \\n World’

Leave a Reply

Your email address will not be published. Required fields are marked *