Python language basics 14: string basics

Introduction

You can think of a string as being a text. It is a combination of characters like “a” or “h”. Characters can just as well be non-Latin ones of course such as Greek, Cyrillic, Chinese or Japanese characters. Characters that make up a word, a sentence or a whole book are all examples of strings.

Creating strings in Python

There are various ways to create strings in Python. We’ve already seen some examples in this series. You just put the string within single or double quotes and you’ve constructed a string:

myString = "hello world, this is Python calling"
myOtherString = 'hello world, this is Python calling'

You can put together different string elements to form a single string. This is called string concatenation:

myString = "hello " "world " "of " "Python"

myString will resolve to “hello world of Python”

What if you’d like to put a quotation mark in the string? You can do like this:

myString = "he said: \"Hello world\""

myString will be he said: “Hello world”

You’ll notice the backslash \ character in front of the quotation mark. That’s an example of character escaping. Another example of character escaping is if you want to put a backslash in your string:

myString = "This is a \\ in the text"

The \ character is escaped by itself which results in the double backslash within the quotation marks.

You can read more about character escaping in general here. You can read more about character escaping in Python here.

You can produce multi-line strings in at least two ways. One way is by using the special “\n” newline character within the string:

myString = "this\nis\na\nmultiline\nstring"

myString will look as follows when printed:

this
is
a
multiline
string

You might think it’s ugly to put those newline characters directly within the string. The triple quotes come to the rescue:

myString = """this
is
multiline"""

We can also mention a special way to create raw strings in Python. You can put an “r” in front of the string and the string will be evaluated literally:

myRawString = r"This is a raw \\ string with \ slashes "

myRawString will be This is a raw \\ string with \ slashes .

Immutability

When talking about strings we have to mention object immutability. At first it sounds scary but if an object is immutable it means that its state cannot be changed. Strings are immutable in many popular languages like C# and Java. Python is no exception. It can be confusing because at first site strings can be changed rather easily:

myString = "hello"
myString = "world"
myString = "goodbye"

We’ve just changed myString a couple of times, right? Well, not really. myString is only a variable that points to the string “hello” in memory. We then make myString point to a different string “world”. The original string “hello” is still “hello”, it hasn’t suddenly changed to “world”. The same is true of “goodbye”. At the end of this process we’ll have 3 strings in memory and myString will point at “goodbye”. I.e. when it’s printed or evaluated in any other way its value will be “goodbye”.

You can read more about object immutability in programming here. We haven’t talked about objects much in this series yet but we’ll do so in great detail.

We’ll look at some other facets of strings in the next post.

Read all Python-related posts on this blog here.

Advertisement

About Andras Nemes
I'm a .NET/Java developer living and working in Stockholm, Sweden.

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 )

Connecting to %s

Elliot Balynn's Blog

A directory of wonderful thoughts

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

WEB APPLICATION DEVELOPMENT TUTORIALS WITH OPEN-SOURCE PROJECTS

Once Upon a Camayoc

Bite-size insight on Cyber Security for the not too technical.

%d bloggers like this: