Useful String Methods in Python

Learn about some of Python’s built-in methods that can be used on strings

A string is a sequence of characters. The built-in string class in Python represents strings based upon the Unicode international character set. Strings implement the common sequence of operations available in Python along with some of the additional methods which are exclusive to them. The figure below shows all of these available methods :

Built-in String Functions in Python

In this article, we shall learn about some of the more frequently used ones. Something important to note here is that all the string methods always return new values and do not change or manipulate the original string.

The code for this article can be accessed from the associated Github Repository, or you can view it on my binder by clicking the image below.


1. center( )

The center() method center aligns a string. The alignment is done using a specified character (whitespace is the default)

Syntax

str.center(length, fillchar), where :

  • length is the length of the string[Required]
  • fillchar is the character which specifies the alignment[Optional]

Example


2. count( )

The count() method returns the count or the number of times a particular value appears in a string.

Syntax

str.count(value, start, end), where :

  • value is the substring which is to be searched in the string[Required]
  • start is the starting index within the string where the search for the specified value starts[Optional]
  • end is the ending index within the string where the search for the specified value ends[Optional]

Example


3. find( )

The find()method returns the lowest index of a particular substring in a string. If the substring is not found, -1 is returned.

Syntax

str.find(value, start, end), where :

  • value or substring which is to be searched in the string[Required]
  • start is the starting index within the string where the search for the specified value starts[Optional]
  • end is the ending index within the string where the search for the specified value ends[Optional]

Types

rfind() : The rfind() method is similar to find() except that it returns the highest index value of the substring

Example


4. swapcase( )

The swapcase() method returns a copy of the string with all its uppercase letters converted into lower case and vice versa.

Syntax

string.swapcase()

Example


5. startswith( ) and endswith( )

The startswith() method returns True if the string starts with the specified value; otherwise, it returns False.

The endswith() function, on the other hand, returns True if the string endswith the specified value, else it returns False.

Syntax

string.startswith(value, start, end)

string.endsswith(value, start, end)

  • value is the string to look for in the string[Required]
  • start is the starting index within the string where the search for the specified value starts[Optional]
  • end is the ending index within the string where the search for the specified value ends[Optional]

Example


6. split( )

The split() method returns a list of words in a string where default separator is any whitespace.

Syntax

string.split(sep, maxsplit)

  • sep: The separator to be used for splitting the string. if nothing is specified, whitespace is the default separator[Optional]
  • maxsplit: denotes the number of splits. Default is -1 which means “all occurrences”[Optional]

Version

  • rsplit(): splits a string from the right.

Example


7. String Capitalization

1. capitalize( )

The capitalize() method capitalizes only the first character of a string.

Syntax

string.capitalize()

2. upper( )

The upper() method capitalizes all the letters of the string.

Syntax

string.upper()

3. string.title( )

The title() method capitalizes all the first letters of the given string.

Syntax

string.title()

Example


8. ljust( ) and rjust( )

The ljust() method returns a left-justified version of the given string using a specified character, whitespace being default. The rjust() method aligns the string to the right.

Syntax

string.rjust/ljust(length, character)

  • length: length of the string which is to be returned[Required]
  • character: Character used for filling in the missing space where whitespace is default[Optional]

Example


9. strip( )

The strip() method returns a copy of the string with the leading and trailing characters removed. Default character to be removed is whitespace.

Syntax

string.strip(character)

character: set of characters to be removed[Optional]

Versions

  • rstrip(): strips characters from the right of a string.
  • lstrip(): strips characters from the left of a string.

10. zfill( )

The zfill() method adds zeros(0) at the beginning of the string. The length of the returned string depends on the width provided.

Syntax

string.zfill(width)

  • width: specifies the length of the returned string. However, no zeros are added if the width parameter is less than the length of the original string.

Example


Conclusion

These were some of the useful built-in string methods in Python. There are others which are not mentioned in the article but are equally important. The Python documentation is an excellent resource if you are thinking to go deeper into the details.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s