Python String:

Strings are formed when a list of characters come together in order. Python for data science lacks a character data type. A string with a length of 1 is a single character. Using square brackets, you can call the elements of a string.

Creating Strings:

We can use single, double or triple quotes to create strings in Python.

You cannot hold a single quote character in a single quote string, an error occurs as the compiler is unable to recognize the beginning and the end. We can use double quotes to house single-quote character in them. Similarly, double-quote characters can be housed in triple quote characters. Triple quotes also help in the creation of multiline strings.

Understanding Strings in Python for Data Science - PST Analytics

Accessing characters in Python:

Indexing is the method there for accessing characters in Python. Slicing, on the other hand, is there to access a range of characters. Slicing operator (colon) is there for slicing a string. In indexing negative reference addresses can be there to call the characters from the end, e.g. -1 will refer to the last character and -2 to the second last character and so on. When an address out of the range is given, an ‘IndexError’ message is shown. A TypeError occurs when we use index type other than an integer.

Understanding Strings in Python for Data Science - PST Analytics

Deleting/Updating from a string:

The deletion or updation of character in a string is not allowed in Python. Although a whole string can be deleted using the built-in del keyword. Strings are rigid and cannot be changed. The only thing which can be done is assigning a new string to the same name.

Updating a character in String:

Understanding Strings in Python for Data Science - PST Analytics

Updating the entire String:

Deletion of Character:

Deleting entire String:

The del keyword is there in Python to delete the entire string.

Understanding Strings in Python for Data Science - PST Analytics

Escape sequencing in Python:

Syntax Error Occurs while printing with single and double quotes as we have already seen. So, to print such strings either we use triple quotes or escape sequence.

A backslash begins the escape sequence, and it is interpreted in a different way. In the case of single quotes being there for representing a string, we need to escape all the single quotes which we started and the same is done for Double Quotes. If we don’t want to consider the escape sequence we use, r or R which implies it is a raw string.

Formatting of Strings:

The format() method is there to format strings in python for data science. This method consists of curly braces {} as place holders which hold arguments according to position or keyword to specify the order.

Format specifiers can make a string left(<), right(>) or center(^), separated by a colon(:). Format specifiers can be there to round off or display Integers such as binary, hexadecimal, etc. and float in the exponential form.

In the old style of formatting, format method was not there, instead % operator was there.

To learn more, you can check this and this.

Leave a Reply

Your email address will not be published. Required fields are marked *