Learn Python - 4 [Strings, Text]

in python •  7 years ago 

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.

>>> 'Hello, World'
'Hello, World'
>>> x = 'My name is Green Turtle.'
>>> x
'My name is Green Turtle.'

As you see above you don't have to assign string to a name, It will print immediately. When you put ' or " around something python knows that you are creating a string.

What if you want to have a multi line string? Multi line string can be created by using three single quotes ''' or three double quotes """.

>>> """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'

The string can contain 'single quotes' or "double quotes" in side a string:

>>> "John says 'Hello to everyone.'"
"John says 'Hello to everyone.'"
>>> 'John says "Hello to everyone."'
'John says "Hello to everyone."'

Note that output string always contains quotes. If you don't want that use print() function:

>>> 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

Strings is an important data types for all programming languages. We will talk about string in next blog posts.

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!