Python Strings with Examples

Strings in Python are sequences of characters that can be easily created. Python Strings are objects of str class. Let’s see what a string is,

Always remember that in Python, single quotes are similar to double quotes. However, triple quotes are also used, but for multi-line string.

Create a String in Python

This is how you can create a string in Python:

String Literals

In Python Strings, literals are enclosed in single, double quotes, and even triple quotes, for example:

The output is as follows:

Above, under variable str3, you can see the string surrounded by three quotes. This forms the string, multi-line.

Slicing to access Substrings

Access characters in a range with Slicing in Python Strings. Let’s say you only want to access substring “Kevin”, for that, we use square brackets. Mention the start index and the end index, separate by colon, to get substring in a range.

For accessing a substring in Python Strings,

The output is,

Negative Indexing to access substrings

If you want to begin slicing from the end of the string, use Negative Indexing. Let us see an example:

The output is as follows:

Accessing a Character in Python Strings

Access a Character from Python Strings is quite easy. Let’s say you only want to access substring “s“, for that, we use square brackets. In some programming languages like C, C++, etc, we have a character type. Under Python Strings, we do not have character type, so strings of length one is considered a character.

For accessing a string character,

The output is,

Escape Characters in Python Strings

The escape characters are used to insert characters, which are not allowed. Use backslash \, since it is used as an escape character. Follow it by the character to be inserted.

Following are the escape characters used in Python:

Escape Characters in Python Strings

Let us now see some examples of the Escape Characters in Python:

Examples of Escape Characters in Python

Following is an example displaying the usage of Escape Characters in Python:

The output is as follows:

Concatenate String in Python

Use the + operator to concatenate String in Python. Let us see an example:

The output is as follows:

String Operators

Python Strings comes with special Operators. Below are these operators, let’s say, we have two variables, with values:

Following are the String operators:

String Operators in Python

Python Built-in String Methods

Let us work on Python Strings Methods:

Python capitalize() method

The capitalize() method in Python, as the name suggests, is used to capitalize (uppercase) only the first letter of  sentence. Let us see an example:

The output is as follows:

Let us see another example to implement the capitalize() method:

The output is as follows:

Python casefold() method

The casefold() method in Python is used to convert a string into lowercase. Let us see an example:

The output is as follows:

Let us see another example to implement the casefold() method:

The output is as follows:

Python center() method

The center() method in Python is used to align a string centrally. You can also use any character as a fill character each size, but this is optional. The syntax is as follows:

Above, len is the length of the string, whereas, char is the character to filled on left and right side.

Let us see an example to implement the center() method:

The output is as follows:

Let us see another example to implement the center() method:

The output is as follows:

Python count() method

The count() method in Python is used to search for a specific value and its count of appearance in the string. The syntax is as follows:

Above, str is the string, begn is the position to start the search and end is the position to end.

Let us see an example to implement the count() method on Python:

The output is as follows:

Let us now see another example to implement the count() method:

The output is as follows:

Python encode() method

The encode() method in Python encodes the string. Default is UTF-8. Let us see an example:

The output is as follows:

Python find() method

The find() method in Python is used to search for the occurrence of a specific letter in a string. The syntax is as follows:

Above, str is the string to be searched for, begin is where to start the search and end is where the search ends. Let us see an example:

The output is as follows:

Let us see another example to implement the find() method:

The output is as follows:

Python endswith() method

The endswith() method in Python is used to check for a string. If this string ends with the specified value, TRUE is returned, else False. The syntax is as follows:

Above, str is the value to be checked if the string ends with, begin is the position where the search start, whereas end is where the search ends.

Let us see an example to implement the endswith() method:

The output is as follows:

Let us see another example of the endswith() method:

The output is as follows:

Python expandtabs() method

The expandtabs() method in Python is used to set the tab size. The default tab size, which we all must have used is 8. To set a different tab size i.e. number of whitespaces, use the expandtabs().

Let us see an example, wherein we will set the tab size as the parameter if the expandtabs() method:

The output is as follows:

Let us see another example to implement the expandtabs() method:

The output is as follows:

Python index() method

Use the index() method in Python to find the index of the first occurrence of a specific value. Let us see the syntax:

Above, str is the value to be searched, begin is where the search begins and end is where it ends.

Let us now see an example of index() method in Python:

The output is as follows:

Let us see another example of index() method with search beginning from a specific index:

The output is as follows:

Python isalnum() method

The isalnum() method in Python returns TRUE, if all the characters in the string are alphanumeric, else FALSE is returned.

Let us see an example of the isalnum() method:

The output is as follows:

Let us see another example to implement the isalnum() method in Python:

The output is as follows:

Python isalpha() method

The isalpha() method in Python returns TRUE, if all the characters in the string are alphabet, else FALSE is returned.

Let us see an example of the isalpha() method:

The output is as follows:

Let us see another example to implement the isalpha() method:

The output is as follows:

Python isdecimal() method

The isdecimal() method in Python returns TRUE, if all the characters in the string are decimals, else FALSE is returned.

Let us see an example of the isdecimal() method:

The output is as follows:

Python isdigit() method

The isdigit() method in Python returns TRUE, if all characters in the string are digits, else FALSE is returned.

Let us see an example of the isdigit() method:

The output is as follows:

Let us see another example of the isdigit() method:

The output is as follows:

Python isidentifier() method

The isidentifier() method in Python returns TRUE, if the string is an identifier, else FALSE is returned.

Note: Valid identifier doesn’t begin with a number. It doesn’t even include spaces. However, a string is a valid identifier, if it includes (a-z), (0-9), or (_).

Let us see an example of the isidentifier() method:

The output is as follows:

Let us see another example to implement isidentifier() method:

The output is as follows:

Python islower() method

The islower() method in Python returns TRUE, if all the characters in the string are lowercase, else FALSE is returned.

Let us see an example of the islower() method:

The output is as follows:

Let us see another example to implement the islower() method:

The output is as follows:

Python isnumeric() method

The isnumeric() method in Python returns TRUE, if all the characters in the string are numeric, else FALSE is returned.

Let us see an example of the isnumeric() method:

The output is as follows:

Let us now see another example to implement the isnumeric() method:

The output is as follows:

Python isprintable() method

The isprintable() method in Python returns TRUE, if all the characters in the string are alphabet, else FALSE is returned.

Let us see an example of the isprintable() method:

The output is as follows:

Let us see another example to implement the isprintable() method:

The output is as follows:

Python isspace() method

The isspace() method in Python returns TRUE, if all the characters in the string are whitespaces, else FALSE is returned.

Let us see an example of the isspace() method:

The output is as follows:

Let us see another example to implement the isspace() method:

The output is as follows:

Python istitle() method

The istitle() method in Python eturns TRUE, if all the string considers the rules of a title (i.e. uppercase title), else FALSE is returned. Uppercase titles, example, “Demo Text”, “Mark”, etc.

Let us see an example of the istitle() method:

The output is as follows:

Let us see another example to implement the istitle() method:

The output is as follows:

Python isupper()method

The isupper() method in Python returns TRUE, if all the characters in the string are uppercase, else FALSE is returned.

Let us see an example of the isupper() method:

The output is as follows:

Let us see another example to implement the isupper() method:

The output is as follows:

Python join() method

The join() method in Python join is used to join the iterable elements to the end of the string. Containers, such as Tuple, String, Dictionary, etc. are iterables in Python.

Let us see an example of the join() method, wherein we have Dictionary iterable, returning keys:

The output is as follows:

Let us see an example of the Strings iterable with Python join():

The output is as follows:

Python lower() method

The lower() method in Python is used to convert the uppercase letters in the string to lowercase.

Let us see an example of the lower() method:

The output is as follows:

Let us see another example to implement the lower() method in Python:

The output is as follows:

Python lstrip() method

The lstrip() method in Python is used to remove characters from the left of the string. Default is whitespace. The syntax is as follows:

Above, the parameter ch are the characters to be trimmed.

Let us see an example of the lstrip() method:

The output is as follows:

Python replace() method

The replace() method in Python is used to replace a value with another. The syntax is as follows:

Above, old is the value to be searched, new is the value to be replaced with old, and count is how many occurrences of the old value to be replaced.

Let us see another example:

The output is as follows:

Let us see another example to implement the replace() method:

The output is as follows:

Python rfind() method

The rfind() method in Python is used to find the last occurrence of a value from a string. The syntax is as follows:

Above, val is the value to be searched, begin is from where the search begins, and end is where it ends.

Let us see an example to implement the rfind() method:

The output is as follows:

Let us see another example to implement the rfind() method:

The output is as follows:

Python rindex()method

The rindex() method in Python is used to get the index of the last occurrence of a specific value. The syntax is as follows:

Above, val is the value to be searched, begin is from where the search begins, and end is where it ends.

Note: rindex() is the same as rfind()

Let us see an example to implement the rindex() method in Python:

The output is as follows:

Let us see another example to implement the rindex() method:

The output is as follows:

Python rjust() method

The rjust() method in Python is used to right align the string and fill characters to the left. The syntax is as follows:

Above, len is the total length of the string to be returned, whereas chars are the characters to be files on the left.

Let us see an example to implement the rjust() method in Python:

The output is as follows:

Let us see another example to implement the rjust() method:

The output is as follows:

Python rsplit() method

The rsplit() method in Python is used to split a string, beginning from the right. A list is returned. The syntax is as follows:

Above, sep is the separator used while splitting, and split is the number of splits to be performed. If you won’t mention anything, that would mean “all occurrences”.

Let us see an example to implement the rsplit() method in Python:

The output is as follows:

Note: Above, since we mentioned 2 as a parameter for split, the output is two elements i.e.:

Let us see another example to implement the rsplit() method in Python:

The output is as follows:

Python rstrip() method

The rstrip() method in Python is used to remove characters from the right of the string. Default is whitespace. The syntax is as follows:

Above, the parameter ch are the characters to be trimmed.

Let us see an example of the rstrip() method:

The output is as follows:

Let us see another example to implement the rstrip() method and remove whitespace from the right:

The output is as follows:

Python split() method

The split() method in Python is used to split a string into a list. You can also set the separator. Whitespace is the default separator. The syntax is as follows:

Above, sep is the separator used while splitting, and split is the number of splits to be performed. If you won’t mention anything, that would mean “all occurrences”.

Let us see an example of the split() method, wherein 2 elements are returned after split:

The output is as follows:

Note: Above, since we mentioned 2 as a parameter for split, the output is two elements i.e.:

Python splitlines() method

The splitlines() method in Python is used to split a string into list. You can also set option to allow line breaks or not. The syntax is as follows:

Above, linebrk is a boolean value to set whether to allow line breaks or not.

Let us see an example to implement the splitlines() method:

The output is as follows:

Let us see another example to implement the splitlines() method:

The output is as follows:

Let us see another example to implement the splitlines() method:

The output is as follows:

Python startswith() method

The startswith() method in Python returns TRUE, if string begins with a particular value., else FALSE is returned. The syntax is as follows:

Above, val is the value to be checked where the string begins with, begin is the position where the search begins, and end is the position where the search ends.

Let us see an example to implement the startswith() method in Python:

The output is as follows:

Let us see another example to implement the startswith() method:

The output is as follows:

Python swapcase() method

The swapcase() method in Python is used swap the case i.e. convert lowercase to uppercase, and vice versa.

Let us see an example to implement the swapcase() method in Python:

The output is as follows:

Let us see another example to implement the swapcase() method in Python:

The output is as follows:

Python title() method

The title() method in Python is used to convert the first letter of every work lettercase i.e. title, heading, etc.

Let us see an example to implement the title() method in Python:

The output is as follows:

Let us see another example to implement the title() method:

The output is as follows:

Python upper() method

Use the upper() method in Python to convert the lowercase letters in the string to uppercase.

Let us see an example of the upper() method:

The output is as follows:

Let us see another example to implement the upper() method in Python:

The output is as follows:

Python zfill() method

The zfill() method in Python is use to fill zeros in the beginning of the string. The syntax is as follows:

Above, the parameter len is the total length of the string including the zero fill.

Let us see an example to implement the zfill() method in Python:

The output is as follows:

Let us see another example to implement the zfill() method:

The output is as follows:

Python strip() method

The strip() method in Python is used to remove spaces from the beginning and end of a string. The syntax is as follows:

Above, the parameter ch are the set of characters to be removed from the beginning and end of the string.

Let us see an example to implement the strip() method in Python:

The output is as follows:

Let us see another example to implement the strip() method in Python and removing leading and trailing characters:

The output is as follows:

In this guide, we saw how to work with Python Strings, accessing a specific character, escape characters and the built-in string methods.

Python Tutorial (English)

If you liked the tutorial, spread the word and share the link and our website Studyopedia with others.


For Videos, Join Our YouTube Channel: Join Now


Read More:

Scope of Variables in Python
Python DateTime Tutorial with examples
Studyopedia Editorial Staff
contact@studyopedia.com

We work to create programming tutorials for all.

No Comments

Post A Comment

Discover more from Studyopedia

Subscribe now to keep reading and get access to the full archive.

Continue reading