Sunday, April 20, 2025

Python Concatenation, Types

To concatenate left side (our custom report) with right side, which is variable that hold string, we will use plus (+) symbol:


name = "John"
lastname = "Snow"
years = "39"

print("Name: " + name)
print("LastName: " + lastname)
print("Years: " + years)

We can also use a comma:


name = "John"
lastname = "Snow"
years = 39

print("Name: ", name)
print("LastName: ", lastname)
print("Years: ", years)

To see with what we are dealing with (data type detection), there's type() function:


name = "John"
lastname = "Snow"
years = 39

"""
print("Name: ", name)
print("LastName: ", lastname)
print("Years: ", years)
"""

print(type(name))
print(type(lastname))
print(type(years))

Result:


<class 'str'>
<class 'str'>
<class 'int'>
>>> 

We are working with two strings (name, lastname) and one number (integer).

Sometimes, we will put result of type() function in dedicated variable, so it can be used in further operations.

We will use that approach all the time:


name = "John"
lastname = "Snow"
years = 39

"""
print("Name: ", name)
print("LastName: ", lastname)
print("Years: ", years)
"""

result = type(name)
print(result)

This will be result in Shell:


<class 'str'>
>>> 

It's so easy to work with numbers in Python. Let's learn about that in next tutorial.

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...