Python. Functions for converting strings and single characters




Functions for converting strings and single characters. Functions int(), str(), repr(), float(), ord(), chr()


Contents


Search other websites:

1. Function int(). Converting to integer

The int() function is the constructor of the int class. This function returns an integer object that can be formed from:

  • integer number or object;
  • a number or a floating point object;
  • a character string or string object containing a sequence of numbers.

A function can be called in one of two ways:

  • int() – in this case, the function returns a value of 0;
  • int(x, base) – in this case, the object x is processed. The base value defines the calculus system into which the x object must be converted. By default, base = 10. If the calculus system is explicitly specified, then the type of the object x must be lowercase, other types are prohibited in this case.

Example.

# Function int()
# 1. Call without parameters
a = int() # a = 0

# 2. Call with an integer value
a = int(15) # a = 15

# 3. Call with a floating point number
a = int(2.85) # a = 2

# 4. Call with string
s = '28'
a = int(s) # a = 28

# 5. The call to conversion to another number system - only for strings

# convert 155 from 16th system to decimal
s = '155'
a = int(s, 16) # a = 341, value 155 in the 16th system

# convert 111 from binary to decimal
s = '111'
a = int(s, 2) # a = 7

# 6. You can not do it this way
# a = int(111,2) - error, you cannot convert a number, there must be a string

 

2. Function str(). Converting to string

The str() function is the constructor of the str class. The function returns an object as a string. The function is used to convert from one type (for example, numeric) to a string type.

The function has two implementations:

  • str() – without parameters. In this case, the function returns an empty string;
  • str(x) – with parameter x. The parameter x is an object of some type. This call may also contain two additional parameters that define the encoding system (the encoding system is set to utf-8 by default) and the error handling scheme (the UnicodeError exception call scheme is set by default).

The result of the str() function can be used for output in the print() function.

Example.

# Function str()
# 1. Call with an integer
x = 25
s = str(x) # s = '25' - string of characters

# 2. Call with floating point
y = 2.338
s = str(y) # s = '2.338'

# 3. Call with string
s = str("3.778") # s = '3.778'

# 4. A call with a string that does not display a number
s = str("abcdef") # s = 'abcdef'

# 5. Call without parameters
s = str() # s = ''

 

3. Function repr(). Convert to string as literal

The repr() function implements conversion to a string in this way if this string were a literal in the program code.

The repr() function can be applied to:

  • an integers;
  • a floating point numbers;
  • a strings.

Example 1. Convert an integer to a string that is represented by a literal.

# Function repr()
# convert an integer to a string
number = 25
snumber = repr(number) # snumber = '25' - as string of characters

number = -100029
snumber = repr(number) # snumber = '-100029' - as string of characters

Example 2. Convert a floating point number to a string.

# Function repr()
# convert a floating point number to a string
number = 25.233
snumber = repr(number) # snumber = '25.233'
snumber = repr(-1e-2) # snumber = '-0.01'

Example 3. Convert a character string to another string as if it were a literal.

# Function repr()
# convert string to string
s1 = 'Text string'
s2 = repr(s1) # s2 = "'Text string'"

s1 = "Text string"
s2 = repr(s1) # s2 = "'Text string'"

Example 4. Convert a tuple.

# Function repr() - tuple processing
x = ( 2.85, 15, 'Hello')
str_x = repr(x) # str_x = "(2.85, 15, 'Hello')"


 

4. What is the difference between the functions of repr() and str()?

In Python, the repr() and str() functions convert arbitrary objects to their string representation. The difference between the functions is manifested in the way they convert the string.

The repr() function returns a string object representation as a line of program code that can be executed to recreate this object. When using such an object in the print() function, quotation marks enclosing the string (a string in enclosed quotes) will be displayed.

In turn, the str() function is used to convert a string to a string without converting it to the previous form. When using the result of the str() function in the print() function, a regular string in single quotes will be displayed.

For example.

# Functions repr() and str()
# convert string to string
s1 = 'Text string'

# function repr()
s2 = repr(s1) # s2 = "'Text string'"

# function str()
s3 = str(s1) # s3 = 'Text string'

As you can see from the above code, the repr() function returns a string as a literal

"'Text string'"

and str() returns a regular string

'Text string'

 

5. Function float(). Convert to floating point

The float() function returns a floating point number that can be formed from another number or from a string. A function can be called in one of two ways:

  • float() – without parameter. In this case, a value of 0.0 is returned;
  • float(x) – where the x parameter specifies the number or string to be converted.

As an input parameter or initial result, a function can use grammar symbols:

  • “Infinity” or “inf” – represent infinity. Infinity can be negative: “-Infinity” or “-inf”;
  • “nan” – denotes uncertainty.

Example.

# Function float() - returns a floating point number
# 1. Results obtained from the number
a = 25 # an integer
x = float(a) # x = 25.0

b = 21.555e-2 # a floating point number
x = float(b) # x = 0.21555

# 2. The result is obtained from the string
# s = 'abcdef' - error, the string does not have the form of a number
# x = float(s)

# string in exponential notation
s = '-1.23e-2'
x = float(s) # x = -0.0123

# string in its original format
s = "288.75"
x = float(s) # x =   288.75 - as a number

# use in expression
x = float(5.2)+float(3.8)*float(-2.1) # x =   -2.7799999999999994

# use with a + sign
x = float('+2.34') # x = 2.34

# use with the word "infinity" or "inf"
x = float("Infinity") # x = inf
x = float('-inf') # x = -inf - as a negative infinity

# use with the word "nan"
x = float("nan") # x = nan

x = float() # x = 0.0

 

6. Functions ord() and chr(). Conversion of character codes

The ord() and chr() functions are used for single characters. These functions are inverse to each other.

The chr() function allows you to get a character based on its code. The ord() function allows you to get code based on the representation of a character.

The general form of calling the chr() function is as follows

c = chr(i)

where c is the character representation of the character with Unicode code i.

The general form of calling the ord() function

i = ord(c)

where i is the character code of c.

Example.

# Functions chr() and ord()
# 1. Функция chr()
code = 49 # decimal code
symbol = chr(code) # symbol = '1' - digit '1'

code = 100
symbol = chr(code) # symbol = 'd'

code = 7785
symbol = chr(code) # symbol = 'ṩ' - Unicode decoding

# 2. Function ord()
symbol = '5'
code = ord(symbol) # code = 53

symbol = 'ṩ'
code = ord(symbol) # code = 7785

symbol = ' ' # the space character
code = ord(symbol) # code = 32

 

7. Is it possible in Python to add a string with a number?

No, it is not. If you need to add a number in the form of a string, then the corresponding conversion function is used for this. If you need to add two strings (string concatenation), then the number is converted to a string using the corresponding function.

Example.

# Add a string with a number will not work
s = '25' # this is a string
a = 30 # this is a number

# Case 1. Add (sum) two numbers
# First you need to convert the string to a number
b = int(s)
res = a + b
print('As numbers: a + s = ', res)

# Case 2. Add two strings
# First you need to convert a number to a string
res_str = str(a) + s
print('As strings: a + s = ', res_str)

The result of the program

As numbers: a + s = 55
As strings: a + s = 3025

 


Related topics