String

Strings in Python

In the previous blog Data Types, we explored Strings in Python, and in this blog let’s dig a bit deeper into strings and what we can do with them.
A string in Python is an array (or list) of characters, and in Python, a single character is still considered as a string with only one letter. This is an important distinction as in other programming languages, there is the “char” datatype which stores only one character.
Consider a string with the text “Verification Master”, and look at the image below to see their respective index numbers.

Since a string in python is an array we access it by specifying the index number.

string_variable = "Verification Master"

print(string_variable[5]) # This should print "i"

If we want to access a set of characters, we simply add the index of the character till which we require.

string_variable = "Verification Master"

print(string_variable[3:14])

The output would be

NOTE: If you are still confused why “a” wasn’t printed as well, remember that in a range if the endpoint is “n” then Python stops at “n-1”. Here n is 14, and so Python will stop at index 13.

We can see the length of a string using the len() function. The len() function prints the number of items in a string.

string_variable = "Verification Master"

print(len(string_variable)) # This would print 19

We can also use Python to check if a certain text (sub-string) is contained within a larger text.

sample_string = "Python; simple to learn programming language"

print("simple" in sample_string) # This should print True

Strings functions

Python offers a range of built-in functions that we can use with strings, and so we won’t be able to explore every one of these, but we will explore a select few to understand how to accurately use such a function.
capitalize(): Allows us to capitalize the first letter of the text.

sample_string = "python is a simple to learn programming language"

sample_string = sample_string.capitalize()
print(sample_string)

Here we are capitalizing the first letter of the string and storing the capitalized result in the same variable.

  • islower()/isupper(): Checks if the string is in lowercase/uppercase respectively.
test_string = "python"

print(test_string.islower())
print(test_string.isupper())

This would print True first and then False, as test_string contains a value that is in lowercase.

  • count(): Counts the number of occurrences of a sub-string within a string. The sub-string can even contain only one string.
sample_string = "python is a simple to learn programming language"

count = sample_string.count("py")
print(count) # Prints 1
    • count(value, start, end)
      • value – This is the sub-string we want to find in the original string. (mandatory)
      • start – This parameter allows us to start our search from a specific index. (optional)
      • end – This parameter allows us to end our search at a specific index. (optional)
count = sample_string.count("py", 3, 15)
print(count) # Prints 0 since we are only starting the search after index 3
  • lower()/upper(): Converts the string to lowercase/uppercase characters respectively.
test_string = "PyThoN"

print(test_string.upper())
print(test_string.lower())

  • index(): Provides us the index of the sub-string

Consider the following table which covers a few more of the available built-in string functions.

Serial No. Function Description
1. endswith Returns True if the string ends with the specified argument provided
2. find Works similar to the index(), however, if the specified substring is not found it returns -1, whereas the index() function raises an error.
3. isalnum Returns True only if all characters in the string are either alphabets or numbers (space, !, #, etc. will make the function return False).
4. isalpha Returns True only if all characters in the string are alphabets.
5. isdigit Returns True only if all characters in the string are digits.
6. isidentifier Returns True only if the string is an identifier.
7. isprintable Returns True only if all characters are printable.
8. isspace Returns True only if there is whitespace in the string.
9. join Joins all items in a collection, and separates them based on the separator we provide.
10. replace(old, new) Replaces all the substrings that have old with new.
11. split Splits the string into different elements of a list at the specified separator at all occurrences of the separator within the string.
12. strip Removes all the occurrences of the specified value within a string, if not specified whitespace will be removed.
13. title Converts the starting letters of a string to uppercase and the rest to lowercase.
14. zfill Adds 0’s to the string/number till the length of the string becomes the specified length which we passed as an argument.

 

What have we learned?

.How to use a select letter of a string?
.How to see the length of a string?
.How to check if a substring exists in a string?
.How to check the index value of a substring?
.How does the find() differ from the index()?

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments