Strings in Python can be defined as a sequence of characters. They are immutable, meaning they cannot be modified once they are declared. Instead, a copy of the string is created for manipulation purposes.
How to Create Strings in Python
Creating strings in Python is as easy as assigning value to a variable in Python. You can use single quotes (’ ‘), double quotes (" “), or three single(’’’ ‘’’) /double quotes(”"" “”") to create strings.
Output:
The advantage of using a double quote for creating a string is that you can use a single quote character inside the double quote. Python will treat the single quote character as a part of the String.
Output:
If you want to create a multiline string, then using three single quotes(’’’ ‘’’) / three double quotes(""" “”") is the best choice. While creating strings using single quotes (’ ‘) or double quotes (" “), you need to use \n escape character for a new line (line break). But by using three quotes, you don’t need to do that.
Output:
How to Access String Characters
If you want to access individual characters, then Indexing is used; if you want to access a range of characters, then Slicing is used.
String Indexing
Just like any other Python data types, string indexes start with 0. The range of indexes is from 0 to length of the string - 1. Python strings also support negative indexing: -1 points to the last character of the string, -2 points to the 2nd last character of the string and so on.
Output:
You must use integers to access characters otherwise, you will get a TypeError. This will also happen if you will try to access elements that are out of range.
TypeError:
Output:
IndexError:
Output:
String Slicing
You can access a range of characters using the colon operator ( : ).
Output:
How to Use Operators on Strings
Using the + Operator
The + operator is used to concatenate/join two or more strings. It returns the resultant concatenated string.
Output:
Using the * Operator
This is used to repeat a string a given number of times.
Output:
Using the in Operator
This is a membership operator which checks if the first operand is present in the second operand or not. If the first operand is present in the second operand then it returns True.
Otherwise it returns False.
Output:
Using the not in Operator
Another membership operator, not in works opposite to the in operator. If the first operand is present in the second operand, it returns False. Otherwise it returns True.
Output:
Escape Sequences in Strings
Using the escape sequences you can place special characters in the string. All you need to do is add a backslash (/) just before the character you want to escape. If you don’t escape the character, Python throws an error.
Output:
How to Insert Variables in Strings
Variables can be used inside the strings by interpolating variables in curly braces. Also, you need to add a lowercase f or uppercase F just before opening the quote of the string.
Output:
How to Use Built-in String Functions
len() Function
This function is used to find the length of the string. It is one of the most used functions in Python.
Output:
ord() Function
Meanwhile this function is used to find the integer value of a character. Python is a versatile language, it supports ASCII as well as Unicode characters.
Output:
chr() Function
Use chr() to find the character value of an integer.
Output:
str() Function
Use this function to convert any Python object to a string.
Output:
How to Join and Split Strings in Python
Splitting a String
You can use the split() method to split the string into a list of strings based on a delimiter.
Output:
Joining Strings
You can use the join() method to join all the elements of an iterable object. You can use any delimiter you want to join the elements.
Output:
Now You Understand String Manipulation
Dealing with strings and texts is an integral part of programming. Strings act as a medium to communicate information from the program to the user of the program. Using Python you can manipulate the strings the way you want.