Python language basics 14: string basics
May 23, 2015 Leave a comment
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 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:
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 .
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.