Strings in Python

In Python string is an immutable object and can be written within double quotes or single quote.


#!/usr/bin/python3
def main():
    strings = "I love you."
    print(strings)
    anotherStrings = "I love you but\nI don't know how much you love me."
    print(anotherStrings)
if __name__ == "__main__":
    main()

And here is the output:

I love you.
I love you but
I don’t know how much you love me.

As you see, we used a back slash to get a new line. And we got an exact break where we needed it.
There is also raw string output. See this code:


#!/usr/bin/python3
def main():
    strings = "I love you."
    print(strings)
    anotherStrings = "I love you but\nI don't know how much you love me."
    print(anotherStrings)
    rawStrings = r"I love you but\nI don't know how much you love me."
    print(rawStrings)
if __name__ == "__main__":
    main()

And here is the output:

I love you.
I love you but
I don’t know how much you love me.
I love you but\nI don’t know how much you love me.

The last statement is called raw string, where back slash is not working any more and we get a raw output. And it is used in regular expression. We will discuss it in detail in our regular expression chapter.
We can insert integer into the middle of a string. I show you both the methods used in Python 2 and Python 3 but remember you better stick to the construct used in Python 3.
Let us first see the Python 2 code:


days = 8
lyrics = "%s days a week is not enough to love you." %days
print(lyrics)

The output:

8 days a week is not enough to love you.

Let us now see the Python 3 code:


days = 8
lyrics = "{} days a week is not enough to love you."
print(lyrics.format(days))

The output:

8 days a week is not enough to love you.

What is the major difference between these two constructs? The difference is in the latest version of Python, we treat string as an object. Hence “lyrics” object used a method called format() and passed a paramater that it wanted to format into it. In the line print(lyrics.format(days)) we used period – “.”, to call the method format() which is in built in the string class.
In your coding life you need to use plenty of strings and some of them might have multiple line breaks. You can not use back slash ‘n’ each time. It is cumbersome.
There is a trick you can use in Python to use multiple new lines.


newLines = """\
first line
second line
thiord line
more to come...
"""
print(newLines)

The output:

first line
second line
thiord line
more to come…

Now you can use single quote instead of double quotes. You can use no back slash at the beginning but that will generate a space in the beginning of the line.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s