Introduction to Strings in Python

Before you dive into string functions in Python, it is essential that you are well-versed with the exact definition of what a string is.

In simple terms, a string is a data type that is used to describe textual content rather than numbers in different programming languages. It is primarily composed of a predefined collection of characters, including whitespace and numeric digits, and is usually enclosed in single or double quotes to make it stand out throughout the execution of a program.

The string data type in Python is represented as <'str'> and is delimited using single or double quotation marks.

String literals in Python do not have an upper bound on their maximum length and only depends on your computer's memory resource.

Some examples of valid strings in Python are as follows:

  • "Learn By Doing"
  • "24 * 7"
  • "Crio.Do"

In Python, indices can be used to access individual characters in a string. Python also enables users to access characters from inside a string using both positive and negative address references, which is unique. Positive indices grant you access to characters from the beginning of the string, while negative indices let you access characters from the end.

The string data type in Python comes with a large set of predefined methods. These functions allow modifying and working with string literals in Python a breeze.

For example, if you want to convert every character of your string to lowercase, there's a better way of doing it rather than iterating through every character of the string.

It can be easily achieved in a single line using the lower() method in Python which does the exact same job. That's how useful these methods are in real life.

.

.

This article will focus on the top 15 in-built string methods in Python which have some amazingly useful applications in string manipulation in data structures and algorithms.

By the end of this read, you will have a solid and practical understanding of the various string methods used in solving algorithmic problems in Python.

Whether you are an absolute Python beginner or a seasoned developer, this blog will set you up with the right level of foundational understanding you need to tackle the exercise problems.

For example, if you want to verify whether every character of a string in Python is alphanumeric [A-Z, a-z, 0-9] or not, there's a better way of doing it than this:

str1="LearnByDoing123"
f=0
for i in str1:
    if not ((i>='A' and i<='Z') or (i>='a' and i<='z') or (i>='0' and i<='9')):
        f=1;
print("Not alphanumeric") if f==1 else print("Alphanumeric")

The better approach would be to leverage Python's predefined isalnum() method to check whether the string is alphanumeric or not. Here's the revamped approach:

str1="LearnByDoing 123"
f=str1.isalnum()
print("Not alphanumeric") if f==False else print("Alphanumeric")

See just how much simpler the code becomes when you leverage the isalnum() method. This is just a peek into what you're about to explore inside this blog.
.

.

.

So, are you ready with your pen and paper to learn the usage of the most useful string functions? Let’s jump in and solve simple problems to understand the working of every string method covered in this blog.

capitalize()

The capitalize() string method in Python returns a new modified string by converting the first character of the original string to uppercase and simultaneously converting other characters to lowercase, if any.

Yes, it's as simple as that.

Usage

  • Syntax
<string_name>.capitalize()
  • Parameter(s)

No parameters required.

Activity 1

  • Suppose you have a list of strings as given below:
['crio COMMUNITY', '89 TIMes', 'learn by doing']

Using the concept you just learned, PREDICT the modified array if capitalize() function is used.

.

Figured it out? Check below to see if you nailed it or not.

['Crio community', '89 times', 'Learn by doing']

Activity 2

  • Now, write a simple Python program that accepts the list given above and returns the modified list using the function discussed here.

Can't figure out a solution? Try understanding the solution given below.

lt = [ 'crio COMMUNITY', '24 TIMes', 'learn by doing' ]
rt = [ ]

for i in lt:
    rt.append (i.capitalize())
print(rt)

P.S: Don't forget to share your approach with us in the comments below.


casefold()

In Python, the casefold() string method returns a string by converting all characters to lowercase, if any.

Although this method is quite similar to the lower() method which does the same job, it differs in the aspect that it is more aggressive in its conversion by taking many different languages into account.

The German lowercase letter 'ß', for example, is the equivalent to 'ss'.

The lower() function, however, does not convert ß because it is already a lowercase German alphabet.

BUT, casefold() function converts it into 'ss' due to its aggressive approach.

Usage

  • Syntax
<string_name>.casefold()
  • Parameter(s)

No parameters required

Activity 1

  • Suppose you're given a list of strings as given below:
['crio Devs', 'ß Community', 'Project BASED']

Using the concept you just learned, PREDICT the modified array if casefold() function is used.

.

Figured it out? Check below to see if you got it right or not.

['crio devs', 'ss community', 'project based']

Activity 2

  • Now, write a simple Python program that accepts the list given above and returns the modified list.

.

HINT: Use casefold() function for each list item.

Still can't figure out a solution? Look below to see how it's achieved:

lt=['crio Devs', 'ß Community', 'Project BASED']
rt=[]

for i in lt:
    rt.append(i.casefold())

print(rt)

P.S: Don't forget to share your approach with us below.


count()

The count() string method in Python counts and returns the number of times a substring appears in a given string.

It's quite simple actually. Now take a brief look at its syntax below.

Usage

  • Syntax
string.count( value, start_pos, end_pos)
  • Parameter(s)
    • value: Substring to be searched in the given string
    • start_pos: Starting index of string from where substring search begins [ 0-indexing ]
    • end_pos: Final index of string where substring search ends [ 0-indexing ]

Activity 1

  • Suppose you're given two strings as defined below:
string1= 'Crio focuses on project-based learning. Crio encourages learn by doing'
string2= 'Crio'

Using the concept you just learned, PREDICT the number of occurrences of the substring (string2) in 'string1' after the punctuation (full-stop)

.

Simple right? Take a peek at the solution below just to be sure:

Number of Occurrences: 1

Activity 2

  • Now, write a simple Python program using two strings defined above and return the same output as you just saw.

.

Stuck? Take a look at the implementation below:

string1 = 'Crio focuses on project-based learning.Crio encourages learn by doing'
string2 = 'Crio'

res=string1.count(string2, 38, len(string1)-1)
print("Number of Occurrences: ", res)

encode()

In Python, the encode() string method encodes a given string according to the specified encoding standard. In case no encoding standard is provided, 'UTF-8' is the default encoding standard used.

Usage

  • Syntax
<string_name>.encode(encoding = encoding, errors = errors)
  • Parameter(s)
    • encoding: This parameter represents the encoding standard used (default is UTF-8)
    • errors: This parameter represents the errors to ignore or replace. There are six basic types of error responses, namely:
      • strict - It is the default response that raises a UnicodeDecodeError exception on failure.
      • ignore - This error response ignores the unencodable characters from the resultant string.
      • replace - This error response replaces the unencodable character with a question mark.
      • xmlcharrefreplace - This error response places the unencodable character with an XML character.
      • backslashreplace - Inserts a backslash instead of an unencodable Unicode.
      • namereplace - This error response replaces the unencodable character with a text explaining the character.

But do remember this. Both are optional parameters.

Activity 1

  • Suppose you're given a string in Python which is defined as follows:
string1= 'I am Job-Reådy'

Using the concepts explored above, PREDICT which "errors" parameter will produce the following outputs:

  1. I am Job-Re\N{LATIN SMALL LETTER A WITH RING ABOVE}dy
  2. I am Job-Re?dy
  3. I am Job-Redy

.

Facing difficulties?

We strongly recommend that you go back and review the six basic types of error responses explained above before you look through the solution below.

Solution:

  1. namereplace [ The unencodable character 'å' has been replaced with a text explaining the character. ]
  2. replace* [ The unencodable character 'å' has been replaced with a question mark*. ]
  3. ignore [ The unencodable character 'å' has been ignored while encoding. ]

Activity 2

  • Now, write a simple Python program that encodes the string above ( just copy it as given ) and prints the three separate encoded versions using the requisite error flags. [ CONSTRAINT: encoding = 'ascii' ]

.

Stuck? Take a look at the implementation below. It's very simple:

string1 = 'I am Job-Reådy'

print ( string1.encode( encoding = "ascii", errors = "namereplace" ) )
print ( string1.encode( encoding = "ascii", errors = "replace" ) )
print ( string1.encode( encoding = "ascii", errors = "ignore" ) )

Got a different example you wish to share? Share it with your fellow learners in the comments below.


endswith()

In Python, the endswith() string method checks whether a given string ends with a specific suffix or not and returns either true or false based on the verdict.

Usage

  • Syntax
< string_name >.endswith(suffix, start, end)
  • Parameter(s)
    • suffix: This parameter represents the string suffix to be checked for.
    • start: Starting index of string from where suffix search begins [0-indexing]
    • end: Final index of string from where suffix search ends [0-indexing]

Activity 1

  • PREDICT the outputs of the three print() statements defined below:
string1 = "Crio Dev Community."

result = string1.endswith('ity.', 5)
print(result)

result = string1.endswith('Dev', 4, 8)
print(result)

result = string1.endswith('Crio Dev', 11)
print(result)

.

Found the solution? Check below to see how you performed.

True
True
False
Don't forget that the start and end indexes are 0-based if you're still in a pickle.

Activity 2

  • Write a Python program to accept two strings from the user and return true if the second string is the suffix of the first string.

.

Can't figure out a solution?

We strongly recommend that you go back and review the basics of the string method explained above before you glance at the solution below.

Solution

string1 = input("Enter string: ")
string2 = input("Enter suffix to be searched: ")

print(string1.endswith(string2))

find()

In Python, the find() string method returns the position/index of the first occurrence of a substring in a given string. Else, it returns -1. That's it.

Usage

  • Syntax
<string_name>.find( value, start, end )
  • Parameters
    • value: This parameter represents the substring to be searched
    • start: Starting index of string from where substring search begins [0-indexing]
    • end: Final index of string from where substring search ends [0-indexing]

Activity 1

  • Write a simple Python program to accept a string from the user and print true if there are no vowels present in the string. [ Use find() method ]

.

Can't figure out the solution?

.

Here's a small hint: String method find() returns -1 if a substring does not exist in a given string.

.

Still can't figure out the solution. Take a look at its implementation below:

string1 = input("Enter a string: ")
​
list1=['a', 'e', 'i', 'o', 'u']
f=0
​
for i in list1:
   if string1.find(i)!=-1:
      f=1
      break
​
if f==1:
   print("False")
else:
   print("True")

Share your approach to these activities with us in the comments below.


format()

In Python, the format() string method returns the position/index of the first occurrence of a substring in a given string. Else, it returns -1. That's it.

Usage

  • Syntax
<string_name>.format( k0=v0, k1=v1, k2=v2,.….kn=vn )
  • Parameters
    • kn: This parameter represents the keyword argument defined inside the placeholder.
    • vn: This parameter represents the value assigned to the keyword argument and replaced inside the placeholder.

Example Implementation

txt1 = "My name is {first_name}. I'm from {country}".format (first_name="Rick", country="India")
print(txt)

Output

 My name is Rick. I'm from India

Notice how the placeholder values are replaced by the argument values defined inside brackets. That's what the format() method does.

Formatting Types

Some of the different formatting types used are as follows:

  • :< -> Left aligns the result(within available space)
  • :> -> Right aligns the result (within available space)
  • :^ -> Center aligns the result (within available space)
  • :, -> Use a comma as a thousand separator
  • :_ -> Uses underscore as thousand separator

There are a lot more formatting separators as such. But you won't be covering all the separators here. Let's move on to a few activities.

Activity 1

  • Write a simple Python code to accept a numeric string (consisting of digits) from the user and format it using the comma separator.

.

Can't figure out the solution?

.

Here's a small hint: Go back and review the five formatting types mentioned above.

.

Still can't figure out the solution. Take a look at the implementation below:

intval = int(input("Enter a string: "))
 
print("{:,}".format(intval))

Activity 2

  • Write a simple Python program to accept a numeric string (consisting of digits) from the user and format it using the underscore separator.

.

Can't figure out the solution?

.

Here's a small hint: Go back and review the five formatting types mentioned above. The hint's still there.

.

Still can't figure out the solution. Take a look at the implementation below:

intval = int(input("Enter a string: "))
 
print("{:_}".format(intval))

Share your approach to these activities with us in the comments below.


join()

In Python, the join() string method joins each item of an iterable (such as a list, tuple, or string) by a user-defined string separator and then returns the concatenated string.

Usage

  • Syntax
<string_name>.join(iterable)
  • Parameters
    • Iterable: This parameter represents a string, list, tuple or dictionary in Python.
  • Example Implementation
myList = [ 'Revive', 'Your', 'Dev', 'Career' ]
mySeparator = '#'
res = mySeparator.join(myList)
print(res)
  • Output
 Revive#Your#Dev#Career

Notice how the list items are concatenated using the '#' separator  as defined above.

Activity 1

  • Suppose you're given a dictionary of items below:
myDict = {"Company": "Crio", "Designation": "Developer"}

What will be the concatenated string if the dictionary items are concatenated using the join() command? [ CONSTRAINT: Use '#' separator ]

.

HINT: join() command works on the keys of a dictionary.

.

Still can't figure out the solution? Here's what your modified string will look like.

Company#Designation

Activity 2

  • Write a simple Python code to achieve the same result as displayed above.

.

Pretty simple right?..........Wait, are you stuck? Okay, take a quick glance at the implementation below.

myDict = {"Company": "Crio", "Designation": "Developer"}
mySeparator = "#"
res = mySeparator.join(myDict)
print(res)

ljust()

In Python, the ljust() string method accepts a string and an integer (width) and returns the left-aligned string of the given width.

Usage

  • Syntax
<string_name>.ljust(length, character)
  • Parameters
    • length: This parameter represents the length of the new string.
    • character: This parameter represents the character to fill the missing space (to the right of the given string) [default is space]
  • Example Implementation
txt = "Project"
x = txt.ljust(10, "1")
print(x)
  • Output
Project111

Notice how the new string length is 10 and the extra space to the right of the original string has been padded with 1s.

Activity 1

  • Write a simple Python program that accepts the following from the user:
    • A string
    • An integer that represents the new width of the original string.
    • A character that represents the space to be filled to the right of the original string.

Print a left-justified version of the string just entered using the ljust() command.
.
So how do you do it? It's simple right?You'll only be provided with hints to solve this problem.
.
.
Hint 1: Use ljust() function in Python3.

Hint 2: Take a close look at its parameters. You'll see that the user inputs match the required parameters.

Still unsure about this concept? Share your issues with your fellow learners in the comments below.


rjust()

In Python, the rjust() string method accepts a string and an integer (width) and returns the right-aligned string of the given width.

Usage

  • Syntax
<string_name>.rjust(length, character)
  • Parameters
    • length: This parameter represents the length of the new string.
    • character: This parameter represents the character to fill the missing space (to the left of the given string) [default is space]
  • Example Implementation
txt = "Project"
x = txt.rjust(10, "1")
print(x)
  • Output
Project111

Notice how the new string length is 10 and the extra space to the left of the original string has been padded with 1s.

Activity 1

  • Write a simple Python program that accepts the following from the user:
    • A string
    • An integer that represents the new width of the original string.
    • A character that represents the space to be filled to the left of the original string.

Print a right-justified version of the string just entered using the rjust() command.
.
So how do you do it? It's simple right?You'll only be provided with hints to solve this problem.
.
.
Hint 1: Use rjust() function in Python3.

Hint 2: Take a close look at its parameters. You'll see that the user inputs match the required parameters.


partition()

In Python, the partition() string method searches for a specified substring inside a string and splits the original string into a tuple containing three elements.

  • The first element contains a part of the string before the specified substring.
  • The second element contains the specified substring.
  • The third element contains the remaining part after the string.

Usage

  • Syntax
<string_name>.partition(value)
  • Parameters
    • value: This parameter represents the substring to be searched for.
  • Example Implementation
myStr = 'Personalised Career Guidance'
myValue = 'reer'
res = myStr.partition(myValue)
print(res)
  • Output
('Personalised Ca', 'reer', ' Guidance')

Notice how the original string has been split into a tuple as expected.

Activity 1

  • Suppose you're given a string and a substring to be searched as given below:
str1 = 'Crio Dev Community' #Original string
str2 = 'Project' #Substring to be searched

PREDICT the new tuple if the original string is partitioned using the second string as a separator.

.

Can't figure out the solution? You may need to explore documentation further to decipher the solution.

.

Still stuck? Take a look at the final answer below.

( 'Crio Dev Community', '', '' )

Here's a new point to remember: partition() method returns the original string and two empty strings as a tuple in case the substring search is unsuccessful.

Activity 2

  • Write a simple Python code to accept two string arguments and partition the first string with respect to the second string.

.

Pretty simple right?.......... Wait, are you stuck?

.

Here's a hint to get you going:  Use the partition() string method in Python.

Still stuck? Well here's the Python solution for your reference.

txt = input("Enter a string: ")
sep = input("Enter separator string: ")
res = txt.partition(sep)
print(res)

Don't forget to share your approach with us in the comments below.


replace()

In Python, the replace() string method returns a modified copy of the string where each occurrence of a substring is replaced by another string.

Yes, it's as simple as that.

Usage

  • Syntax
<string_name>.replace(old_value, new_value, count)
  • Parameters
    • old_value: This parameter represents the substring to be searched for.
    • new_value: This parameter represents the new substring to be replaced with.
    • count: (Optional) This parameter represents the number of occurrences of 'old_value' to be replaced.
  • Example Implementation
myStr = 'Crio focuses on project-based learning. Crio encourages learn by doing'
old_value = 'Crio'
new_value = 'Crio.Do'
res = myStr.replace(old_value, new_value)
print(res)
  • Output
Crio.Do focuses on project-based learning. Crio.Do encourages learn by doing

Notice how the original substring has been replaced by a new substring as defined above.

Activity 1

  • Find out what happens if the number of occurrences is not specified as the third parameter in replace() function.

.

Hint: Look for the default value associated with this parameter.

Activity 2

  • Write a Python program that accepts two strings and replaces the first N occurrences of the first string with the second string. Print the modified string.
  • Input:
    • str1 -> Original string
    • str2 -> Substring to be replaced
    • str3 -> Substring to be replaced with
    • N -> Number of occurrences to be replaced

It's highly recommended that you go back and review the concept explained above before you solve this problem.

.

In case you're still struggling, check the implementation below.

str1 = input("Enter a string: ")
str2 = input("Enter a substring to be searched: ")
str3 = input("Enter new substring to be replaced with: ")
n = int(input("Enter number of occurrences: "))
res = str1.replace(str2,str3,n)
print("Modified string: ", res)

It's as simple as that. Okay, let's move on to the next string method.


split()

In Python, the split() string method splits a given string at a specified separator and returns a list of separated strings.

Yes, it's that simple.

Usage

  • Syntax
<string_name>.split(separator, split_num)
  • Parameter(s)
    • separator: This parameter represents the separator in use while splitting the original string.
    • split_num: This parameter represents the number of splits you wish to achieve.
  • Example implementation
txt = "Blogs. Projects. Learning."
x = txt.split(".")
print(x)
  • Output
['Blogs', ' Projects', ' Learning', '']

Notice how the original string has been split at the full-stop separator into a list of strings.

Activity 1

  • Find out what happens if the count of the number of splits is not specified as the parameter in the split() function.

.

Hint: You may need to look further into the documentation for the split() function to find the solution.

Activity 2

  • Write a simple Python program that accepts a string from the user and splits the original string N times at a user-defined separator. Print the modified string.
  • Input:
    • str1 -> Original string
    • ch -> Separator to be split at
    • N -> Number of string splits

.

It's highly recommended that you go back and review the concept explained above before you solve this problem.

In case you're still struggling, check the implementation below.

str1 = input("Enter a string: ")
ch = input("Enter a character separator: ")
n = int(input("Enter number of splits: "))
res = str1.split(ch,n)
print("Modified string: ", res)

Substrings in Python

Since you're exploring string splitting at the moment, it would be prudent to go over substrings in Python.

In Python, a substring is defined as a sequence of characters that is present within another string.

Usage

  • Syntax
string[start: end: step]
  • Parameters
    • start: This parameter represents the starting index of the substring
    • end: This parameter represents the ending index of the substring. The character at this index is not included in the substring.
    • step: This parameter represents every step character after the current character to be included in the substring.

Activity 1

  • Suppose you're given a string as follows: str1 = "DevCommunity". PREDICT the output for the following slicing instructions:

    • str1[ 0 : 5 ]
    • str1[ 2: 7 ]
    • str1[ -1 ]
    • str1[ -5 : ]
      .
      Facing difficulties?
      It's recommended that you explore the official documentation for the Python substring function further before attempting these problems.
      .
      .
      Done? All right. Let's go over the solutions.
  • Solutions:

    • DevCo
    • vComm
    • y
    • unity

The understanding and reasoning behind these solutions are left as an exercise for you.


strip()

In Python, the strip() string method returns a modified copy of the string by removing all leading and trailing characters as defined by the user.

That's it!

Usage

  • Syntax
<string_name>.strip(characters)
  • Parameter(s)
    • characters: This parameter represents the characters to be stripped from the beginning and end of a given string.
  • Example implementation
string1 = "         Crio Community.    "
x = string1.strip()
print(x)
  • Output
Crio Community.

Notice how the original string has been stripped off of its leading and trailing spaces.

Activity 1

  • PREDICT the output of the following code:
string1 = '  devs love devs   '
print(string1.strip())

.

It's pretty simple, right? Check if your solution matches ours below.

devs love devs

Activity 2

  • PREDICT the output of the following piece of code in Python:
string1 = 'devs love devs'
print(string1.strip('de'))

.

This might be a bit tricky.

The idea is that strip() method removes all occurrences of the characters 'd' and 'e' from the beginning and end of the string. Hence our result is something like this:

vs love devs

zfill()

In Python, the zfill() string method pads the beginning of a string with zeros until a specified length is reached.

Yes, it's quite a simple concept.

Usage

  • Syntax
<string_name>.zfill(len)
  • Parameter(s)
    • len: This parameter represents the width of the modified string with zeros padded to its left.
  • Example implementation
string1 = "Project"
x = string1.zfill(10)
print(x)
  • Output
000Project

Notice how the original string has been padded with 5 zeros to its beginning.

Activity 1

Find out what happens if the value of the parameter is less than the length of the original string.

HINT: You may need to look further into the documentation of the zfill() function to find a solution.

Activity 2

  • PREDICT the output of the following Python program:
str1 = "+1000"
res = str1.zfill(10)
print (res)

.

Here's a hint if you're stuck.

HINT: zfill() function adds zeros after prefixing the '+' or '-' sign in the original string.

If that still didn't make sense, take a look at the output below:

+000001000

Notice how the '+' character has been prefixed before padding with zeros. That's something to keep in mind.

Share your approach with your fellow learners in the comments section below.


Here's a table summarising what you just learned about the top 15 string functions in Python.

String Method Syntax Usage
capitalize() <string_name>.capitalize() Returns a string where the first character is upper case.
casefold() <string_name>.casefold() Converts all characters of a string to lowercase.
count() <string_name>.count(value, start_pos,end_pos) Prints the frequency of occurrences of a substring in another given string.
encode() <string_name>.encode(encoding = encoding, errors = errors) Encodes a given string according to the specified encoding standard.
endswith() <string_name>.endswith( suffix, start, end ) Checks whether a given string ends with a specific suffix or not.
find() <string_name>.find( value, start, end ) Returns the position/index of the first occurrence of a substring in a given string.
format() <string_name>.format( k0=v0, k1=v1, k2=v2,.….kn=vn ) Formats the specified value(s) given and inserts them into corresponding placeholders defined inside the string.
join() <string_name>.join(iterable) Joins each item of an iterable (such as a list, tuple, or string) by a user-defined string separator.
ljust() <string_name>.ljust(length, character) Accepts a string and an integer (width) and returns the left-aligned string of the given width.
rjust() <string_name>.rjust(length, character) Accepts a string and an integer (width) and returns the right-aligned string of the given width.
partition() <string_name>.partition(value) Searches for a specified substring inside a string and splits the original string into a tuple of three elements.
replace() <string_name>.replace(old_value, new_value, count) Returns a modified copy of the string where each occurrence of a substring is replaced by another string.
split() <string_name>.split(separator, split_num) Splits a given string at a specified separator and returns a list of separated strings.
strip() <string_name>.strip(characters) Returns a modified copy of the string by removing all leading and trailing characters as defined.
zfill() <string_name>.zfill(len) Pads the beginning of a string with zeros until a specified length is reached.

Hey, seems like you just completed learning about 15 new string methods in Python.

Found this article interesting? Show your love by upvoting this article.

We would also love to know which of the Python string methods got you excited the most - let us know in the comments below.

Also, if you enjoyed reading this article, don't forget to share it with your friends!