I originally published this post on LinkedIn: https://www.linkedin.com/pulse/devasc-200-901-python-strings-riikka-sihvonen/
This is a summary based on the DEVASC 200-901 Official Cert Guide.
The string data type is a sequence of characters inside quotes. You can use single quotes (' ') or double quotes (" "). A string can also include numbers and other characters. However, some characters in Python strings have special use cases.
Example: Create a string.
>>> word = 'Python'
In Python, you don't have to explicitly specify the data type, when you assign a value to an object. Python automatically recognizes the type from the value.
A string is just a list of characters in a certain order that Python keeps track of. Strings are indexed starting from 0. You can retrieve any character in the string by referencing its index.
Example: Find the first character in the string.
You can also pull ranges of characters by using a colon (:). The format is [x:y], where x is the beginning of the slice and y determines the end. Note that the second number (y) is considered "up to but not including", so the last character to be retrieved is the one before the specified index. You can also leave out the start index (to print from the start of the string) or leave out the end index (to print out the end of the string).
Example: Find the characters from index 1 to 4 ("up to but not including").
You can also use negative numbers to indicate the index. If you use a negative number, you start from the end at position -1.
Example: Find the last character in the string.
You can perform math operations on strings as well. The plus (+) operator is used to add or concatenate two strings together.
>>> 'Python' + 'Space'
Multiplication (*) works as well.
>>> 'Fun! ' * 3
>>> 'Fun! Fun! Fun! '
There are many built-in methods in Python that allow you to manipulate the string in different ways.
Example: Convert all characters in the string to upper case.
See: https://docs.python.org/3/library/stdtypes.html (String Methods)
Watch my YouTube video DEVASC - Introduction to Python Strings here: