Strings in python - BOOKC -->

Strings in python

Monday, April 18, 2016

Hello, how are you guys!!! I hope you will be fine and also enjoying this tutorial. In my previous post you learned about  numbers in python in very detailed example. We learned that there are two types of numbers in python. First is "int" and the second one is "float".

We also learned that how to convert "int" into "float" and vice verse. Today you will learn about "strings" in python with example.

Python Strings
        Python has some very powerful facilities for working with strings. And in fact, in Python 3, they become refined, and in some cases even more powerful. Let's get right into strings, and we will start by defining a String (line no ). I will just call it s, and I will say 'This is a string!' (line no 2) and we will go ahead, and we will print(s). And there we have the string.

1. s = 'This is a string'
2. print(s)

Strings in Python are immutable objects, and they're created with either single quotes or double quotes. And so if we change those single quotes to double quotes, we will see that we get exactly the same result.

Python escape character / new line

        You can use Escape characters in a string, as you can in many languages. So if I put in (line no 1) a \n, for example "s = 'This is a\n string.' that will introduce a new line in the middle of the string. And we will print(s), we see that there is a new line now between the a and the s of string. 

1. s = 'This is a\n string'
2. print(s)


 On the other hand, if I want this \n to actually be a part of the string, rather than getting replaced with a new line, I can put the letter r before the definition of the string like that (s = r"This is a\n string."), and if I print(s), I get the \n as part of the string. This is called a raw string, and the place where this gets used the most is when creating regular expressions. And we will get into regular expressions in some detail in another post. The code snippet of above description is given below:

1. s = r'This is a\n string'
2. print(s)


In addition to these sorts of Escapes with the backslashes, you can also do some formatting and replacing of variables in the string (between the string). For example, if I create a number and call this, say "num", and give it a value of 10, I want to put that 10 right in the middle of the string. There are two different ways to do this. The first one is by using the python 3 way and the second is python 2. Firstly I am going to show you the Python 3 way to do this.

def main():
    num = 10
    s = "This is a {} string".format(num)
if __name__ == "__main__": main()

Output: This is a 10 string

Description of above code:
This actually inserts the value of "num" in the middle of the string. We have these curly braces here, and those get replaced with the format. So format is a method of the string object. And so this literal string is actually an object. Remember, everything in Python is an object. We can use this objecty-referencing operator, this period, to access a method of that object, and do this variable replacement. So this is very powerful, and this is very common. And you will see it done this way in new Python 3 code.

And we will look briefly at the Python 2 way to do it as well, because you will see that a lot. For all your new code, you want to use the Python 3 way, which is with the format method of the string object, because the Python 2 way is going to go away. It's considered obsolescent, and it will be dropped in the next version of Python. 

def main():
    num = 10
    s = "This is a %s string" % num
if __name__ == "__main__": main()


 Description of above code:

In Python 2 code, you'll see it's done this other way. I am going to put %s here, and over here a % sign and the letter num. And print(s). You see we get the same result. This is the way that it was done in Python 2. And this is a bit of a hack. It's perfectly valid, and you will see it a lot. You will even see it some in Python 3 code that's written by people who are used to Python 2, and that's fine. The reason you don't want to use this construct is because it is considered obsolescent, and it will be dropped in the next version of Python. So you'll see this, you want to know what it does, but the right way to do it in Python 3 is to use the new format method of the string object.
Accessing values in strings

To access sub string we use square brackets [ ] along with index or indices. For example
>>> s = 'London'
>>> print(s[1:5])

When we run the above code it will produce following result.
Output: L
Output: on


How to concatenation two strings:

The concatenation of strings in python is very simple and easy. For example if we have two strings s1 and s2, and the value of  the first one is "Hello" and the another is "World". And we want to concatenate it we use "+" operator to do this. Code below shows how to concatenate the above strings.

>>> s1 = 'Hello'
>>> s2 = 'World'
>>> print(s1 + s2)

Output: HelloWorld