Python is a relatively simple, implicit and intuitive solution for programming as compared to other programming solutions. Python has a number of functions for manipulating and controlling strings within the scope of any function or in the entirety of the code. A string can be defined as a combination of multiple data types in a single data type. Like multiple woven threads form a string, multiple data types woven together form a string.
There are a number of libraries and built-in keywords that are meant to control strings in their entirety, similar is the case with numbers such as integers and floating points, they too have options for control and manipulation built-in to the Python library.
Strings in Python
A string can be implicitly detected by Python like any other data types and it does not require any special sort of formatting to do so, just a double quote around the string will allow the interpreter to detect the variable assigned to a string. Let’s take a look below:
str1 = "mystring123"
print (str1)
You can also implicitly assign multi-line strings to a variable by simply replacing the double quotes with 3 single quotes or 3 double quotes, let’s take a look:
str2 = """Multi - line string first line,
This is example of second line of multi - line string,
And this is example of third line of multi - line string"""
print(str2)
str3 = '''This is example of first line of multi - line string,
This is example of second line of multi - line string,
And this is example of third line of multi - line string'''
print(str3)
for x in str3:
print(x) // This will loop through the entire string and print each character separately
Apart from just expanding and working with strings, you can apply a variation of functions on strings such as:
Len() in Strings
Since we know that strings act as an array (although there is no such concept in Python, lists and other data structures are used for storing variables). We can apply the length function of strings too.
str3 = "It's a me, Mario!"
print(len(str3)) // this will print the length of the string that is passed to the len function
Searching/Checking Strings
Although proper searching of strings requires the find() function, you can also use the in and not in keywords to check if a certain text is present in the string or not.
str4 = "Apple milkshake and a bowl of snakes"
If "snakes" in str4:
print("Snakes are present") // this will check if the word "snakes" is available in the string, it will print it otherwise it will not.
str5 = "Apple milkshake and a bowl of snakes"
print("snakes" in str5) // this will check if the word "snakes" is available in the string, it will print it otherwise it will not.
str6 = "Apple milkshake and a bowl of snakes"
If "snakes" not in str6:
print("Snakes are not present") // this will check if the word "snakes" is NOT available in the string, it will print it if it is AVAILABLE.
To find a specific word using the find() function, use this command:
str7 = "Hey, you looking fo' dis?"
a = str7.find("looking")
print(a)
Numbers in Python
There are three main data types to manipulate numbers in Python and are as follows:
. Int (Integer): Simple numbers and non-rational
. Float (Floating point): Numbers that can contain decimals and non-rational values.
. Complex: Numbers that have an imaginary part attached to them defined as ‘j’
# Integer
a = 1234
# Floating Point
b = 1122.12
b1 = 50e9 // You can use the 'e' as an indicator to an exponent power
# Complex
c = 12j
You can also produce random values, change types of each passed variable, and perform mathematical operations such as round-off and power.
a = 4545 #int
b = 34.9310 #float
c = 22j #complex
x = complex(a)
y = float(c)
z = integer(b)
print(type(x))
print(type(y))
print(type(z)) // these will print the data types of the variables once the switch is done.
Import random
a = random.randrange(1,5000)
print(a) //You need to import the random library in order to print a random value between the passed range.
In order to perform other mathematical operations, you need to import the math library:
import math
a = 5600
b = 233
c = math.pow(a, 3)
a = 56.4444
b = round(a, 4) //This will round the floating point down 4 decimals only leaving 56 as a whole number, you don't need a module for this.