Strings are one of the data types in Python. Strings are a sequence of quoted characters. They can be assigned simply just like assigning a value to a variable. Strings can also store data types like int, float, etc.
Python also contains a built-in class named “str” which has many useful functions. Python strings are also immutable meaning they cannot be changed after being created, however; strings in Python can be replaced.
String Declaration
Single and double quoted strings are same in Python. They can be created using single, double or triple quotes. Let’s see how you can create a string and assign it to a variable –
Example
Multi-line Strings
Single and double quoted strings are limited to a single line use only. However, triple quoted strings can be extended to multiple lines.
Example
String Manipulation
- Accessing Parts of String
By using indexing, individual characters can be accessed in a string. Python treats the characters as a sub-string because there is no library in Python that is specifically written for characters. This method of accessing a string and dividing it into different parts is called String Slicing. This can be done like this –
Example
- String Concatenation
Concatenation means combining two strings here, it can be done easily using the ‘+’ operator like this –
Example
- Length of a String
In Python, there is a predefined function “len()” to measure the length of the string. The length can be found in this way –
Example
Updating and Deleting a String
As already discussed, Python does not allow you to change a specific part or characters of a string. Python will give you an error because it is not supported. However, you can update or delete an entire string which means that you can reassign a variable to a new string or vice versa.
- Updating a String
Strings can be updated in the following way –
Example
However parts of the string cannot be changed unlike Lists and when you try to do it with the help of indexing, this occurs:-
Example
- Deleting a String: –
Deleting a string removes it from the memory. It can be done by using the predefined ‘del’ function like this –
Example
Escape Sequence
In Python, similar sized quotes can only be used either to declare or can be used inside the string as we do for a normal sentence. To counter this limit, escape sequencing is done. As we know, a string in Python is declared in this way-
Example
A more direct way to avoid this will be of course by declaring a triple quoted string, but sometimes, when some error occurs in a long code, it is not the most efficient way to fix the problem. A similar way is escape sequencing using ‘\’ character –
Example
Formatting Strings
Strings can be formatted by using a great function called format(). This function takes arguments and then formats them. Curly braces ‘{}’ in the string are the placeholders for those passed arguments in the function.
Example
Indexing can also be done to make sure placeholders contains correct values. For example –
Example
In-Built String Functions
Python includes a number of predefined functions for strings. This makes the coding more efficient. We have already discussed format() method/function.
count() – This will return the number of times a specific character or sub-string occurs in the string.
center() – This will return a centralized string
encode() – An encoded version of the string will be returned
index() – This will return the position of a specific value of the string
join() – It will join an iterable to the end of a string
lower() – It will convert the whole string into lower case.
capitalize() – This capitalizes the first character of the string.
partition() – If the string is divided into three parts, it will return a tuple
replace() – This will return the string replaced with a specified value.
strip() – This will return a stripped version of the string
translate() – This will return a translated version of the string
upper() – This will convert the whole string into upper case
find() – This will find some specific vale, and will return its position
s=format_map() – This will format specified value in the given string