Python Arithmetic and String Operations:

arithmetic-and-string-operations-in-python

In this tutorial, we will describe Arithmetic and String Operations in python. These are fundamental to programming in Python.

Python Arithmetic Operations:

Arithmetic operations in python are used for mathematic calculations. Here we are going to describe all the asthmatics operations which are mostly used in python programming.

Addition:

The + operator is used for expansion in Python. It adds two or more values and returns the sum.

a = 10
b = 20
c = a + b
print(c)
# Output: 30

Subtraction:

The – operator is used for subtraction in Python. It subtracts one value from another and returns the difference.

a = 10
b = 20
c = b - a
print(c)
# Output: 10

Multiplication:

The * operator is used for multiplication in Python. It multiplies two or more values and returns the product.

a = 10
b = 20
c = a * b
print(c) 
# Output: 200

Division:

The / operator is used for division in Python. It divides one value by another and returns the quotient. If both operands are integers, the result will be a float.

a = 10
b = 3
c = a / b
print(c) 
# Output: 3.3333333333333335

Modulo:

The % operator is used for modulo or remainder in Python. It divides one value by another and returns the remainder.

a = 10
b = 3
c = a % b
print(c) 
# Output: 1

Floor division:

The // operator is used for floor division in Python. It divides one value by another and returns the quotient rounded down to the nearest integer.

a = 10
b = 3
c = a // b
print(c) 
# Output: 3

By understanding these arithmetics operations you will be able to perform a wide range of mathematical calculations in Python.

Python String Operations:

Here are some everyday string operations in Python along with examples:

String Concatenation:

The + operator is used to concatenate two or more strings. It returns a new string that is a combination of the input strings.

str1 = "Hello"
str2 = "World"
str3 = str1 + " " + str2
print(str3) 
# Output: Hello World

You can also write this code as follow in an easy way.

str1 = "Hello"
str2 = "World"
print(str1, str2) 
# Output: Hello World

String Repetition: 

The * operator is used to repeat a string multiple times. It returns a new string that is a repetition of the input string.

str1 = "Hello "
str2 = str1 * 3
print(str2) 
# Output: "Hello Hello Hello "

String Length:

The len() function is used to get the length of a string. It returns the number of characters in the string.

str1 = "Hello"
length = len(str1)
print(length) 
# Output: 5

String Slicing: 

The [start:end:step] syntax is used to extract a portion of a string. It returns a new string that contains the specified characters.

str1 = "Hello World"
substr1 = str1[0:5]
substr2 = str1[6:]
substr3 = str1[0:11:2]
print(substr1) # Output: "Hello"
print(substr2) # Output: "World"
print(substr3) # Output: "HloWrd"

String Formatting:

The format() method is used to format a string. It returns a new string that is a formatted version of the input string.

name = "Alice"
age = 30
str1 = "My name is {} and I am {} years old".format(name, age)
print(str1) 
# Output My name is Alice and I am 30 years old

Here is all about python arithmetic and string operations, if you want to get more information about python you can subscribe to us.

Was this helpful?
YesNo

Related Articles:

Recent Articles:

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x