Print string with int in python

printing string and int together [duplicate]

I’m getting an error Class ‘str’ does not define ‘__floordiv__’, so the ‘//’ operator cannot be used on its instances . How can I solve this?

input returns a str (string), so you cannot divide it by a number (or another str ). You have to convert it to a number (e.g. int ) first. Surely C++ has types, too?

Hi David, welcome to Stack Overflow. Please, make sure your code is properly formatted when posting. Here you can have a read on how to format your code

And then, once you have integers to operate on and want to print them: How can I concatenate str and int objects?

7 Answers 7

Men = int(input("What's the percentage of men")) Women = int(input("What's the percentage of women")) Men = int(((Men / (Men + Women)) * 100)) Women = int(((Women / (Men + Women)) * 100)) 
print(f"Percentage of men \n Percentage of Women ") 

Men and Women are str variables ( input function returns a string).

  • You should cast them to int after reading from stdin
    • Women = int(Women)
    • Men = int(Men)
    • ((Men//(Men+Women))*100) always returns 0
      • Men < (Men+Women) =>Men/(Men+Women) < 1
      • // operator returns the int part of division => 0
      Women = int((input("What's the number of women?"))) Men = int((input("What's the number of men?"))) print("Percentage of Men: " + str(((Men/(Men+Women))*100)) + "\nPercentage of Women: " + str(((Women/(Men+Women))*100))) 

      You can use the int() function. You can learn more about it here. In this case, this would be the correct code:

      women = int(input("What's the number of women?")) // using int function men = int(input("What's the number of men?")) // using int function print(f"Percentage of Men: \n Percentage of Women: ") 

      I usually using this format

      print "This is man: <>, This is woman: <>".format(man, woman) 

      Using int() to convert to number and f string. Normal division (/) returns a fractional number, whereas floor division (//) truncates the decimal part and returns the quotient.So we are using, / division operator.

      women = int(input("What's the number of women?")) men = int(input("What's the number of men?")) print(f"Percentage of Men: <(men/(men+women))*100>\n Percentage of Women: <(women/(men+women))*100>") 

      As most of the people answered already you need to convert string to desired output. So basically the input function takes any input as str . We should convert it to desired data type.

      Another issue is for the percentage calculation you have used // which is floor division it will always round down the results causing your output always to be 0 .(https://www.w3schools.com/python/trypython.asp?filename=demo_oper_floordiv)

      One last point is if you are using python2 or python3. In python3 using simple division( / ) will work fine with integers. But in python2 it will work like a // if both denominator and numerator are of type (int). Hence you might not get desired results.

      Below code should work fine for both python2 and python3. Because instead of converting input to int we will be converting it to float . Which will work with / properly in both python2 and python3.

      Women = float(input("What's the number of women?")) Men = float(input("What's the number of men?")) print("Percentage of Men: ", (Men / (Men + Women)) * 100) print("Percentage of Women: ", (Women / (Men + Women)) * 100) 

      Источник

      Last updated: Feb 20, 2023
      Reading time · 5 min

      banner

      # Table of Contents

      Use the print() function to print an integer value, e.g. print(my_int) .

      If the value is not of type integer, use the int() class to convert it to an integer and print the result, e.g. int(my_str) .

      Copied!
      my_int = 1234567 # ✅ print integer print(my_int)

      print integer using print function

      We used the print() function to print integer values.

      The print function takes one or more objects and prints them to sys.stdout .

      If you have an integer value, you can directly pass it to the print() function to print it.

      Copied!
      print(100) # 👉️ 100 print(200) # 👉️ 200

      # The print() function returns None

      Note that the print() function returns None, so don’t try to store the result of calling print in a variable.

      Copied!
      my_int = 1234567 # ⛔️ BAD (print always returns None) result = print(my_int) print(result) # 👉️ None

      You can also use a formatted string literal to print an integer.

      Copied!
      my_int = 1234567 result = f'The number is my_int>' print(result) # 👉️ The number is 1234567

      print integer using f string

      Formatted string literals (f-strings) let us include expressions inside of a string by prefixing the string with f .

      Copied!
      my_str = 'The number is:' my_int = 5000 result = f'my_str> my_int>' print(result) # 👉️ The number is: 5000

      Make sure to wrap expressions in curly braces — .

      To print a string and an integer together:

      1. Use the str() class to convert the integer to a string.
      2. Use the addition (+) operator to concatenate the two strings.
      3. Use the print() function to print the result.
      Copied!
      my_int = 100 result = 'The number is ' + str(my_int) print(result) # 👉️ The number is 100

      print integer using addition operator

      We used the str() class to convert the integer to a string.

      This is necessary because the values on the left and right-hand sides of the addition (+) operator need to be of compatible types.

      If you try to use the addition (+) operator with a string and an integer, you’d get an error.

      Copied!
      my_int = 100 # ⛔️ TypeError result = 'The number is ' + my_int

      To get around this, we have to use the str() class to convert the integer to a string.

      When using formatted string literals, we don’t have to explicitly convert the integer to a string because it’s done for us automatically.

      If your integer is wrapped in a string, use the int() class to convert it to an integer before printing it.

      Copied!
      my_str = '1234567' my_int = int(my_str) print(my_int) # 👉️ 1234567

      print integer value using int class

      We used the int() class to convert the integer to a string before printing it.

      The int class returns an integer object constructed from the provided number or string argument.

      Copied!
      print(int('105')) # 👉️ 105 print(int('5000')) # 👉️ 5000

      The constructor returns 0 if no arguments are given.

      You can also use the stdout.write() method from the sys module to print integer values.

      Copied!
      import sys my_int = 123456 # 👇️ The number is 123456 sys.stdout.write('The number is ' + str(my_int))

      print integer using stdout write

      The print() function is actually a thin wrapper around sys.stdout.write .

      The print() function uses the sys.stdout.write method under the hood by default.

      Notice that we used the str() class to convert the integer to a string before using the addition (+) operator.

      This is necessary because the values on the left and right-hand sides of the addition (+) operator need to be of compatible types.

      Alternatively, you can pass multiple, comma-separated arguments to the print() function.

      Copied!
      my_int = 100 # 👇️ The number is 100 print('The number is ', my_int, sep='') # 👇️ The number is 100 print('The number is ', my_int)

      print integer using commas

      We passed multiple, comma-separated arguments to the print() function to print a string and an integer.

      By default, the sep argument is set to a space.

      By setting the argument to an empty string, no extra whitespace is added between the values.

      Copied!
      print('a', 'b', 'c', sep="_") # 👉️ "a_b_c" print('a', 'b', 'c', sep="_", end=". ") # 👉️ "a_b_c. "

      The string we passed for the end keyword argument is inserted at the end of the string.

      The end argument is set to a newline character ( \n ) by default.

      This is a two-step process:

      1. Use the str.format() method to interpolate the variable in the string.
      2. Use the print() function to print the result.
      Copied!
      my_int = 247 result = "The integer is <>".format(my_int) print(result) # 👉️ "The integer is 247"

      print integer using str format

      The str.format method performs string formatting operations.

      Copied!
      first = 'Bobby' last = 'Hadz' result = "His name is <> <>".format(first, last) print(result) # 👉️ "His name is Bobby Hadz"

      The string the method is called on can contain replacement fields specified using curly braces <> .

      Make sure to provide exactly as many arguments to the format() method as you have replacement fields in the string.

      The str.format() method takes care of automatically converting the integer to a string.

      # Checking what type a variable stores

      If you aren’t sure what type a variable stores, use the built-in type() class.

      Copied!
      my_int = 123 print(type(my_int)) # 👉️ print(isinstance(my_int, int)) # 👉️ True my_str = 'hello' print(type(my_str)) # 👉️ print(isinstance(my_str, str)) # 👉️ True

      The type class returns the type of an object.

      The isinstance function returns True if the passed-in object is an instance or a subclass of the passed-in class.

      If you need to convert a value to an integer and print the result, use the int() class.

      # Additional Resources

      You can learn more about the related topics by checking out the following tutorials:

      • Print a Dictionary in Table format in Python
      • How to Print on the Same Line in Python
      • How to Print a Horizontal Line in Python
      • How to print Boolean values in Python
      • How to Print a List in Columns in Python
      • Print a List without the Commas and Brackets in Python
      • Print New Line after a Variable in Python
      • How to Print the output of a Function in Python
      • How to Print specific items in a List in Python
      • Print specific key-value pairs of a dictionary in Python
      • How to repeat a String N times in Python
      • How to print Bold text in Python [5 simple Ways]
      • Using nested Loops to print a Rectangle in Python
      • How to print a Timestamp for Logging in Python

      I wrote a book in which I share everything I know about how to become a better, more efficient programmer.

      Источник

      How to print string and int in the same line in Python

      This Python tutorial is on how to print string and int in the same line in Python. This is a very common scenario when you need to print string and int value in the same line in Python.

      Printing string and integer value in the same line mean that you are trying to concatenate int value with strings.

      Hey the value of variabe a is 452 You just entered 845

      Here we will see how to concatenate string and int in Python.

      In general, if you run this below Python code:

      a = 5 print ("the value of a is "+a)

      You will get an error like this:

      TypeError: can only concatenate str (not “int”) to str

      Do you know why you are getting an error like this?

      Because you are trying to concatenate an integer value with a string using + operator. In Python, the + operator can only concertante strings as Python is a strongly typed programming language.

      In order to solve this problem and to achieve our goal to concatenate strings with int we can use these below methods:

      Python code to concatenate a string with an int

      a = 5 print ("the value of a is "+str(a))
      $ python codespeedy.py the value of a is 5

      Here we have used str() method to convert the int value to int.

      There are other techniques too to achieve our goal.

      We can also use comma to concatenate strings with int value in Python

      a = 5 print ("the value of a is ",a)
      $ python codespeedy.py the value of a is 5

      Concatenate integers in Python

      In this way, we can concatenate two or more integers in Python

      Источник

      Читайте также:  Python read file as hex
Оцените статью