Concate str with int or numbers using f

Posted under » Python on 08 June 2022

A simple string concatenate in Python.

goyayfeels = '/feels/'+ str(goyay) +'/' 

However, if you try to concatenate string and int using + operator, you will get a runtime error.

One way to get around this is by using the format() function

 call = requests.get(f'http://api.com/sid/A{user_id + 3}-2')

In Python, string and numbers don't mix, so this will work

salty = str(entry['uid']) 
call = requests.get(f'http://api.com/sid/{salty}')

Where user_id is an integer. However, this only works in Python 3.6 and above.

Conversely, you need to convert text to numbers. Assuming, var q2go is a string, then you need to

percent = int(q2go)/total * 100

If you want to display leading zero you need to format it.

number = 1
print(f"{number:02d}")

web security linux ubuntu python django git Raspberry apache mysql php drupal cake javascript css AWS data