Basics of Strings in Python
Strings in python are used to save text information like names, phrases, comments etc. Strings are a sequence of characters, which means python keeps a track of every character in a string in a specific order. This helps to perform indexing operations in strings.
In this article we will learn about following:
- Creating a string
- Printing a string
- Indexing and Slicing
- String Properties
- String Methods
Creating a String:
To create a string in python we use either single quote or double quotes. Let’s see a few examples:
We have to be careful while using single and double quotes in strings. In the above example, the error occurs because single quote in Let’s stopped the string. To overcome this problem we can use a combination of single and double quotes.
Printing a String:
In Jupyter Notebooks, declaring a string in a cell automatically gives a string as output. But the correct way to output a string is using ‘print’ statement.
We can do this using print statement.
String Indexing and Slicing:
Strings in python are sequential, which means each element of a string can be accessed using indexing.
Indexing in python starts from ‘0’. First, a string is assigned to an object, then we call the object and use to call its index.
Let’s start indexing.
To calculate the length of string we use an built-in python function len(). The length of a string includes spaces and punctuations as well.
For slicing, python uses ‘:’ which grabs everything up to a specified index. Let’s see few examples.
In the above slicing, python grabs everything from index 0 up to index 3, but not including index 3. The output has index 0, 1 and 2. The concept here is “up to, but not including”.
We can also use negative indexing in strings, this helps in getting a reverse string.
The most important property of strings is immutability. This means that once a string is created, it can’t be changed, altered or replaced.
Another property of strings is concatenation. This means we can add two separate strings and print them as one.
But this will not affect the actual string ‘a’ which is ‘Hello World!!’. To reassign the string after concatenation we can follow the following method:
We can also perform multiplication on strings to create repetitions.
Python has several built-in methods for its objects (strings, lists, dictionaries, etc.). These methods are functions that we can perform on objects.
(We will learn about functions and methods in much more depth after covering all data types and objects.)
The way in which we call a method is:
Jupyter notebook gives a list of methods that are available for a given object type. All we have to do is type the name of object followed by a period and then hit ‘tab’.
Let’s try a few built-in string methods to understand how these methods work.
These are a few basic methods used in strings. Other methods can also be applied in a similar way.
That’s it for this article. Next we will learn about String Formatting.
Till then good bye……………