How to use casting and string in python

How to use casting and string in python

Welcome Guys!

This is another post from the series of Python programming course which is based on casting and String. We have give some brief introducing below with some example which will help you to make a better understanding. So let’s dive in the deep.

Casting

Casting is the conversion of a variable from one form to another. In python coding language, this can be done with functions like int() or float() or str(). It is a very common method which is used to convert a number.

int() method

In int() method, an integer object available in the given number or string treated as a base 10 (with no parameters) and returns 0. If parameters are given then it treats the string with a given base (0, 2, 8, 10, 16).

# integer

integer-method-in-python

print(“int(123) is:”, int(123))

# float

float covert to integer

print(“int(123.23) is:”, int(123.23))

# string

print(“int(‘123’) is:”, int(‘123’))

When you run the code, you will get the result

use-string-in-python

int(123) is: 123

int(123.23) is: 123

int(‘123’) is: 123

float() method

It is used to get a floating-point number from a number or a string. This method allows to accepts a single parameter which is also not mandatory to use.

The values we use in float method can show the output depending upon the argument.

  • If you put an argument and it got passed, then you will get the result as a floating number.
  • If you put an argument and it doesn’t pass, it will show you 0.0 in result
  • If your provided string is passed (not a decimal number or does not match to any case which you are desired) you will get an error.
  • If a number is passed which is outside the range of python float method it will show an overflow error in the result.

Strings

Strings can be a specific arrangement of characters in Python. They are alluded to as str. You can keep in touch with them by utilizing quotation marks. You can utilize single statements just as twofold statements for composing strings in Python. Strings can be any characters set in quotation marks, for example, ’91’, “hello”, and so on.

Much the same as integers, there is no restriction to character length in strings. Strings are of different sorts, for example, triple-quoted strings, crude strings, and numerous others. Be that as it may, on the off chance that we would begin talking about strings and their sorts, this article will go excessively long.

str() function

It is used to transform any specified value into a string

Example.1

print(str(15))

Result

15

Example.2

# bytes

x = bytes(‘cöde’, encoding=’utf-8′)

It will be good if you learn Data Science course in delhi yourself by joining the PHP Training in Laxmi Nagar

print(str(x, encoding=’ascii’, errors=’ignore’))

Result

It will be good if you learn python course in delhi yourself by joining the Django training in delhi

CDE

So this is all about casting and strings. Hope you have got some idea about the concept. You can slow watch the embedded videos in this post and get a better understanding of it. If you want to learn under the guidance of experts, you can join the best python course in Laxmi Nagar Delhi. For more knowledge wait for our next post and you can also join on our social media platforms.

Spread the love
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •