All string functions in python

An Introduction to String Functions in Python 3

Python has several built-in functions associated with the string data type. These functions let us easily modify and manipulate strings. We can think of functions as being actions that we perform on elements of our code. Built-in functions are those that are defined in the Python programming language and are readily available for us to use.

In this tutorial, we’ll go over several different functions that we can use to work with strings in Python 3.

Prerequisites

You should have Python 3 installed and a programming environment set up on your computer or server. If you don’t have a programming environment set up, you can refer to the installation and setup guides for a local programming environment or for a programming environment on your server appropriate for your operating system (Ubuntu, CentOS, Debian, etc.)

Making Strings Upper and Lower Case

The functions str.upper() and str.lower() will return a string with all the letters of an original string converted to upper- or lower-case letters. Because strings are immutable data types, the returned string will be a new string. Any characters in the string that are not letters will not be changed.

Let’s convert the string Sammy Shark to be all upper case:

Info: To follow along with the example code in this tutorial, open a Python interactive shell on your local system by running the python3 command. Then you can copy, paste, or edit the examples by adding them after the >>> prompt.

ss = "Sammy Shark" print(ss.upper()) 

Now, let’s convert the string to be all lower case:

The str.upper() and str.lower() functions make it easier to evaluate and compare strings by making case consistent throughout. That way if a user writes their name all lower case, we can still determine whether their name is in our database by checking it against an all upper-case version, for example.

Boolean Methods

Python has some string methods that will evaluate to a Boolean value. These methods are useful when we are creating forms for users to fill in, for example. If we are asking for a post code we will only want to accept a numeric string, but when we are asking for a name, we will only want to accept an alphabetic string.

There are a number of string methods that will return Boolean values:

Method True if
str.isalnum() String consists of only alphanumeric characters (no symbols)
str.isalpha() String consists of only alphabetic characters (no symbols)
str.islower() String’s alphabetic characters are all lower case
str.isnumeric() String consists of only numeric characters
str.isspace() String consists of only whitespace characters
str.istitle() String is in title case
str.isupper() String’s alphabetic characters are all upper case

Let’s review a couple of these in action:

number = "5" letters = "abcdef" print(number.isnumeric()) print(letters.isnumeric()) 

Using the str.isnumeric() method on the string 5 returns a value of True , while using the same method on the string abcdef returns a value of False .

Читайте также:  Java work with pdf

Similarly, we can query whether a string’s alphabetic characters are in title case, upper case, or lower case. Let’s create a few strings:

movie = "2001: A SAMMY ODYSSEY" book = "A Thousand Splendid Sharks" poem = "sammy lived in a pretty how town" 

Now let’s try the Boolean methods that check for case:

print(movie.islower()) print(movie.isupper()) 
print(book.istitle()) print(book.isupper()) 
print(poem.istitle()) print(poem.islower()) 

Now we can run these small programs and receive the following output:

Output of movie string
False True
Output of book string
True False
Output of poem string
False True

Checking whether characters are lower case, upper case, or title case, can help us to sort our data appropriately, as well as provide us with the opportunity to standardize data we collect by checking and then modifying strings as needed.

Boolean string methods are useful when we want to check whether something a user enters fits within given parameters.

Determining String Length

The string function len() returns the number of characters in a string. This method is useful for when you need to enforce minimum or maximum password lengths, for example, or to truncate larger strings to be within certain limits for use as abbreviations.

To demonstrate this method, we’ll find the length of a sentence-long string:

open_source = "Sammy contributes to open source." print(len(open_source)) 

We set the variable open_source equal to the string «Sammy contributes to open source.» and then we passed that variable to the len() function with len(open_source) . We then passed the method into the print() method so that we could generate the output on the screen from our program.

Keep in mind that any character bound by single or double quotation marks — including letters, numbers, whitespace characters, and symbols — will be counted by the len() function.

join(), split(), and replace() Methods

The str.join() , str.split() , and str.replace() methods are a few additional ways to manipulate strings in Python.

The str.join() method will concatenate two strings, but in a way that passes one string through another.

balloon = "Sammy has a balloon." 

Now, let’s use the str.join() method to add whitespace to that string, which we can do like so:

We will notice that in the new string that is returned there is added space throughout the first string:

Output
S a m m y h a s a b a l l o o n .

We can also use the str.join() method to return a string that is a reversal from the original string:

Output
.noollab a sah ymmaS

We did not want to add any part of another string to the first string, so we kept the quotation marks touching with no space in between.

The str.join() method is also useful to combine a list of strings into a new single string.

Let’s create a comma-separated string from a list of strings:

print(",".join(["sharks", "crustaceans", "plankton"])) 
Output
sharks,crustaceans,plankton

If we want to add a comma and a space between string values in our new string, we can rewrite our expression with a whitespace after the comma: «, «.join([«sharks», «crustaceans», «plankton»]) .

Читайте также:  Generic class field java

Just as we can join strings together, we can also split strings up. To do this, we will use the str.split() method:

Output
['Sammy', 'has', 'a', 'balloon.']

The str.split() method returns a list of strings that are separated by whitespace if no other parameter is given.

We can also use str.split() to remove certain parts of an original string. For example, let’s remove the letter a from the string:

Output
['S', 'mmy h', 's ', ' b', 'lloon.']

Now the letter a has been removed and the strings have been separated where each instance of the letter a had been, with whitespace retained.

The str.replace() method can take an original string and return an updated string with some replacement.

Let’s say that the balloon that Sammy had is lost. Since Sammy no longer has this balloon, we will change the substring «has» from the original string balloon to «had» in a new string:

print(balloon.replace("has","had")) 

Within the parentheses, the first substring is what we want to be replaced, and the second substring is what we are replacing that first substring with. Our output will look like this:

Output
Sammy had a balloon.

Using the string methods str.join() , str.split() , and str.replace() will provide you with greater control to manipulate strings in Python.

Conclusion

This tutorial went through some of the common built-in methods for the string data type that you can use to work with and manipulate strings in your Python programs.

You can learn more about other data types in “Understanding Data Types,” read more about strings in “An Introduction to Working with Strings,” and learn about changing the way strings look in “How To Format Text in Python 3.”

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Tutorial Series: Working with Strings in Python 3

A string is a sequence of one or more characters (letters, numbers, symbols) that can be either a constant or a variable. Made up of Unicode, strings are immutable sequences, meaning they are unchanging.

Because text is such a common form of data that we use in everyday life, the string data type is a very important building block of programming.

This tutorial series will go over several of the major ways to work with and manipulate strings in Python 3.

Tutorial Series: How To Code in Python

Python banner image

Introduction

Python is a flexible and versatile programming language that can be leveraged for many use cases, with strengths in scripting, automation, data analysis, machine learning, and back-end development. It is a great tool for both new learners and experienced developers alike.

Prerequisites

You should have Python 3 installed and a programming environment set up on your computer or server. If you don’t have a programming environment set up, you can refer to the installation and setup guides for a local programming environment or for a programming environment on your server appropriate for your operating system (Ubuntu, CentOS, Debian, etc.)

Источник

Python String Methods

Python has a set of built-in methods that you can use on strings.

Читайте также:  Fabric language kotlin mod

Note: All string methods returns new values. They do not change the original string.

Method Description
capitalize() Converts the first character to upper case
casefold() Converts string into lower case
center() Returns a centered string
count() Returns the number of times a specified value occurs in a string
encode() Returns an encoded version of the string
endswith() Returns true if the string ends with the specified value
expandtabs() Sets the tab size of the string
find() Searches the string for a specified value and returns the position of where it was found
format() Formats specified values in a string
format_map() Formats specified values in a string
index() Searches the string for a specified value and returns the position of where it was found
isalnum() Returns True if all characters in the string are alphanumeric
isalpha() Returns True if all characters in the string are in the alphabet
isascii() Returns True if all characters in the string are ascii characters
isdecimal() Returns True if all characters in the string are decimals
isdigit() Returns True if all characters in the string are digits
isidentifier() Returns True if the string is an identifier
islower() Returns True if all characters in the string are lower case
isnumeric() Returns True if all characters in the string are numeric
isprintable() Returns True if all characters in the string are printable
isspace() Returns True if all characters in the string are whitespaces
istitle() Returns True if the string follows the rules of a title
isupper() Returns True if all characters in the string are upper case
join() Converts the elements of an iterable into a string
ljust() Returns a left justified version of the string
lower() Converts a string into lower case
lstrip() Returns a left trim version of the string
maketrans() Returns a translation table to be used in translations
partition() Returns a tuple where the string is parted into three parts
replace() Returns a string where a specified value is replaced with a specified value
rfind() Searches the string for a specified value and returns the last position of where it was found
rindex() Searches the string for a specified value and returns the last position of where it was found
rjust() Returns a right justified version of the string
rpartition() Returns a tuple where the string is parted into three parts
rsplit() Splits the string at the specified separator, and returns a list
rstrip() Returns a right trim version of the string
split() Splits the string at the specified separator, and returns a list
splitlines() Splits the string at line breaks and returns a list
startswith() Returns true if the string starts with the specified value
strip() Returns a trimmed version of the string
swapcase() Swaps cases, lower case becomes upper case and vice versa
title() Converts the first character of each word to upper case
translate() Returns a translated string
upper() Converts a string into upper case
zfill() Fills the string with a specified number of 0 values at the beginning

Note: All string methods returns new values. They do not change the original string.

Learn more about strings in our Python Strings Tutorial.

Источник

Оцените статью