Python Lessons – 5.8 Some useful functions for numbers and strings

Python Lessons - 5.8 Some useful functions for numbers and strings

Some useful functions for strings

Python already contains some very useful functions and methods to perform some string operations.

  • join() – concatenates a list of strings with a separator character
  • replace() – replaces one substring with another
  • startswith() – checks if the string starts with a particular sequence of characters
  • endswith() – check if the string ends with a particular sequence of characters
  • upper() – converts all string characters to uppercase
  • lower() – converts all string characters to lowercase
  • split() – splits a string into a list of strings at a separator character

Let’s see some examples

str1 = ",".join(["One","Two","Three"])
print(str1)
str2 = "This is nice".replace("This","That")
print(str2)
bool1 = "This is an apple".startswith("This")
print(bool1)
bool2 = "This is an apple".endswith("apple")
print(bool2)
str3 = "Sentence in lowercase".upper()
print(str3)
str4 = "SENTENCE IN UPPERCASE".lower()
print(str4)
lista = "One,Two,Three".split(",")
print(lista) 

if you run it, you get:

 >>>
One,Two,Three
This is nice
True
True
SENTENCE IN LOWERCASE
sentence in uppercase
['One', 'Two', 'Three'] 

As you can see they are all methods of the string object.

Some useful functions with numbers

As with strings, the basic Python distribution already has a number of useful features when working with numbers. Let’s see some together

  • min () – this function returns the minimum value within a sequence or a list of numbers
  • max () – this function returns the maximum value within a sequence or a list of numbers
  • abs () – calculates the absolute value of a number
  • round () – this function rounds the number for a certain number of decimal places
  • sum () – calculate the sum of a list of numbers

Let’s see some examples together

n1 = min(1,5,0,3,4,6)
print(n1)
n2 = max([1,5,0,3,4,6])
print(n2)
n3 = abs(-12)
print(n3)
n4 = round(3.145,1)
print(n4)
n5 = sum([1.3,5.2,-0.6,3.1,4.4,6.9])
print(n5) 

if you run it, you get

>>>
0
6
12
3.1
20.3   

The all(), any() and enumerate() functions

Python provides some very useful functions regarding lists.

The functions all () and any () are very useful when you want to make all the elements of a list available. In the case of all() it will return True only if all the elements satisfy this condition, in the case of any() it is sufficient that at least one satisfies it.

lista = [33,14,22,64,36]
if all([i > 10 for i in lista]):
    print("All numbers are greater than 10")
if any([i > 50 for i in lista]):
    print("In the list there are values greater than 50") 

if you run it, you get

>>>
All numbers are greater than 10
In the list there are values greater than 50

The enumerate() function is used to perform an iteration for all the elements of a list, having both indices and values as variables to be managed.

lista = [33,14,22,64,36]
for i in enumerate(lista):
    print(i[0],i[1]) 

if you run it, you get

>>>
0 33
1 14
2 22
3 64
4 36

⇐ Go to Python Lesson 5.7 – String Formatting 

Go to Python Lesson 6.1 – Functional Programming ⇒

Leave a Reply