Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,888 questions

51,815 answers

573 users

How to use string format with variables in Python

4 Answers

0 votes
name = "dan"
salary = 12000

s = "The programmer {name} earn ${salary}".format(name=name, salary=salary)

print(s)


'''
run:

The programmer dan earn $12000

'''

 



answered Nov 10, 2018 by avibootz
0 votes
name = "dan"
salary = 12000

s = "The programmer {} earn ${}".format(name, salary)

print(s)


'''
run:

The programmer dan earn $12000

'''

 



answered Nov 10, 2018 by avibootz
0 votes
name = "dan"
salary = 12000

s = "The programmer {name} earn ${salary:,}".format(name=name, salary=salary)

print(s)


'''
run:

The programmer dan earn $12,000

'''

 



answered Nov 10, 2018 by avibootz
0 votes
from string import Template

name = "dan"
salary = 12000

s = 'The programmer $name earn $salary'
s = Template(s).substitute(name=name, salary=salary)

print(s)


'''
run:

The programmer dan earn 12000

'''

 



answered Nov 10, 2018 by avibootz
...