In python Strings are a sequence of characters, enclosed in single quotes '
or double quotes "
.
Why do we need strings? if you want to display some text to someone or “export” out of the program you simply use string.
Almost every programming language support string data type.
<pre><code>>>> 'Hello, World'
'Hello, World'
>>> x = 'My name is Green Turtle.'
>>> x
'My name is Green Turtle.'
<p dir="auto">As you see above you don't have to assign string to a name, It will print immediately. When you put <code>' or <code>" around something python knows that you are creating a string.
<p dir="auto">What if you want to have a multi line string? Multi line string can be created by using three single quotes <code>''' or three double quotes <code>""".
<pre><code>>>> """A multi line string
... this is second line
... this one is third one"""
'A multi line string\n this is second line\n this one is third one'
>>> x = '''Now I am using single
... quotes here'''
>>> x
'Now I am using single\n quotes here'
<p dir="auto">The string can contain 'single quotes' or "double quotes" in side a string:
<pre><code>>>> "John says 'Hello to everyone.'"
"John says 'Hello to everyone.'"
>>> 'John says "Hello to everyone."'
'John says "Hello to everyone."'
<p dir="auto">Note that output string always contains quotes. If you don't want that use <code>print() function:
<pre><code>>>> print("John says 'Hello to everyone.'")
John says 'Hello to everyone.'
>>>x = """A multi line string
... this is second line
... this one is third one"""
>>> print(x)
A multi line string
this is second line
this one is third one
<p dir="auto">Strings is an important data types for all programming languages. We will talk about string in next blog posts.