Python Strings - Machine Learning Tutorials, Courses and Certifications

Python Strings - Machine Learning Tutorials, Courses and Certifications

Machine Learning March 14, 2021 Python Comments Off on Python Strings 5,664 Views

Strings are used in Python to record text information, such as name. Strings in Python are actually a sequence , which basically means Python keeps track of every element in the string as a sequence. For example, Python understands the string “hello’ to be a sequence of letters in a specific order. This means we will be able to use indexing to grab particular letters (like the first letter, or the last letter).

This idea of a sequence is an important one in Python and we will touch upon it later on in the future.

In this lecture we’ll learn about the following:

To create a string in Python you need to use either single quotes or double quotes. For example:

The reason for the error above is because the single quote in I’m stopped the string. You can use combinations of double and single quotes to get the complete statement.

We can also use triple quotes. They function the same way as double and single qoutes.

Just like using double qoutes to use single qoutes without escape characters, we can do the same with double quotes and triple quotes. But its not optimal since you have to put an extra line in between them.

So just use the single quotes around double quotes first.

Escape characters. If there is some where situation where you need a bunch of quotes, you can use escape character \’ \’. Just make sure you close the end of the string without the escape character .

Using Jupyter notebook with just a string in a cell will automatically output strings, but the correct way to display strings in your output is by using a print function.

The combination “\n” makes a newline appear in a string. You can use newlines anywhere you like in a string.

We can also use a function called len() to check the length of a string!

Sidenote: We can check the length of any data types that are collections with the same len() “call”. It is considered a built-in function.

We know strings are a sequence, which means Python can use indexes to call parts of the sequence. Let’s learn how this works.

In Python, we use brackets [] after an object to call its index. We should also note that indexing starts at 0 for Python. Let’s create a new object called s and the walk through a few examples of indexing.

Its important to note that strings have an important property known as immutability. This means that once a string is created, the elements within it can not be changed or replaced. Immuntiblity is a concept seen in other data types.

Immutability means that the data cannot mutate, aka the data cannot change. A new ‘data object’ needs to be created instead.

Notice how the error tells us directly what we can’t do, change the item assignment!

Something we can do is concatenate strings!

Concatenate just means joining two strings together. Its a term used in other programming languages. But, don’t be scared about the terms.

We can use the multiplication symbol to create repetition!

Objects in Python usually have built-in methods. These methods are functions inside the object (we will learn about these in much more depth later) that can perform actions or commands on the object itself. To keep it brief, methods are actions that a ‘data thing’, aka an object like a string, can take. Functions are actions that belong to no ‘data thing’ aka object, and just exist in the program.

We call methods with a period and then the method name. Basically, we say ‘Hey object, do this to these things”. The things in question that we are asking the object to mess with are placed in the parameters .

Methods take the form:

object.method(parameters)

Where parameters are extra arguments we can pass into the method. Don’t worry if the details don’t make 100% sense right now. Later on we will be creating our own objects and functions! But, to keep it brief, parameters represent what the action for the object should take. The arguments are the actual ‘data things’ that it needs to take.

Here are some examples of built-in methods in strings:

We can use the .format() method to add formatted objects to printed string statements.

The easiest way to show this is through an example:

Recommended articles