In order to find out about the type of 'variable', we use the "type" function.
Consider the following example:
variable2 = 'world'
print(type(variable1)) print(type(variable2)) print(type(variable3)) print(type(variable4))
# if we add a float value with an integer we get back an answer in <class 'float'>
#two strings can be concatenated using + operator.
helloworld #It is to be noted that there is no space produced between the two strings
#a string and an integer/float cannot be operated together.
print('hello' + 123)
ERROR: class mismatch
# the integer value in the above example 123 can be converted into a string '123' and then added to concatenate both the strings.
print('hello' + ' ' + '123') # a space can also be added as a string in between.
#To convert a numeriacal string into integer/float we use the syntax:
variable1= '12345' # its declared as a string.
variable2 = int(variable1) #The string is converted into a integer of value 12345.
variable3 = str(variable2) # the integer of value 12345 is now converted into a string '12345'
variable4 = 'hello123' # alphanumeric string is declared
print(int(variable4)) # a string which is not completly numeric cannot be numeric cannot be converted into integer/float.
ERROR: Traceback (most recent call last):
Python/PycharmProjects/ Practice1/***/testing", line 2, in <module>
ValueError: invalid literal for int() with base 10: 'hello123'