Hack Forums
[Python] Adding string after int - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Python] Adding string after int (/showthread.php?tid=6170520)



Adding string after int - Will Patterson - Aug 31, 2021

Code
age = 22
daysalive = (age * 365)
print("Hello, you have been alive for" , daysalive +" days""")



How would I format the print so it does not give errors?:



Traceback (most recent call last):
File "C:\Users\17275\Desktop\Python\daysAlive.py", line 3, in <module>
print("Hello, you have been alive for" , daysalive +" days""")
TypeError: unsupported operand type(s) for +: 'int' and 'str'


Thanks :)


RE: Adding string after int - the craavce - Aug 31, 2021

age = 22
daysalive = (age 365)
print("Hello, you have been alive for " + str(daysalive)+ " days")

or

age = 22
daysalive = (age
365)
print("Hello, you have been alive for " , daysalive , " days")

just replace + with ,


RE: Adding string after int - Will Patterson - Aug 31, 2021

(Aug 31, 2021 08:37 AM)the craavce Wrote:  age = 22
daysalive = (age 365)
print("Hello, you have been alive for " + str(daysalive)+ " days")

or

age = 22
daysalive = (age
365)
print("Hello, you have been alive for " , daysalive , " days")

just replace + with ,

Appreciate it :)


RE: Adding string after int - vorticalbox - Sep 6, 2021

(Aug 31, 2021 08:37 AM)the craavce Wrote:  age = 22
daysalive = (age 365)
print("Hello, you have been alive for " + str(daysalive)+ " days")

or

age = 22
daysalive = (age
365)
print("Hello, you have been alive for " , daysalive , " days")

just replace + with ,

better would be


PHP Code

<?php 
age 
= 22
daysalive 
= (age * 365)
print(
f"Hello, you have been alive for {daysalive} days") 



RE: Adding string after int - L E G I Ø N - Sep 7, 2021

(Sep 6, 2021 01:47 AM)vorticalbox Wrote:  better would be


PHP Code

<?php 
age 
= 22
daysalive 
= (age * 365)
print(
f"Hello, you have been alive for {daysalive} days") 

Even better would be

Code
from datetime import date

birthday = date(1998, 12, 31)
delta = date.today() - birthday
print(f"Hello, you have been alive for {delta.days} days")



RE: Adding string after int - _wiz - Sep 9, 2021

(Sep 7, 2021 12:08 AM)L E G I Ø N Wrote:  Even better would be

Code
from datetime import date

birthday = date(1998, 12, 31)
delta = date.today() - birthday
print(f"Hello, you have been alive for {delta.days} days")

This isn't just better its the only correct answer per pep 498.


RE: Adding string after int - ThinWhiteDuke - Sep 9, 2021

As the saying goes ‘There’s more than one way to skin a cat’ 🙂