0

I am trying to create text input in my HTML form but want that input to have name value set by variable:

#! c:\Python24\python
print "Content-Type: text/html\n" 

import cgi,cgitb
cgitb.enable()

name1 = "kuba"
val1 = 150
name2 = "pipi"
val2 = 300

print("<form action='python2.py' method='GET'>")
print("<input type='text' name='name1'>")
print("<input type=submit value='name2'>")

I would like to have my text to have name "kuba" and not "name1"

Can someone give me a hint? Thank you, Jakub

J.J.
  • 129
  • 2
  • 14
  • 1
    https://stackoverflow.com/questions/18348717/how-to-use-concatenate-a-fixed-string-and-a-variable-in-python – Rafalsonn Aug 10 '18 at 11:05

1 Answers1

1

You could use .format() in your string like this, say name1 is your variable that you want in your second print method. Then you would do:

#! c:\Python24\python
print "Content-Type: text/html\n" 

import cgi,cgitb
cgitb.enable()

name1 = "kuba"
val1 = 150
name2 = "pipi"
val2 = 300

print("<form action='python2.py' method='GET'>")
print("<input type='text' name='{0}'>".format(name1))
print("<input type=submit value='name2'>")

Hence the second print() would output <input type='text' name='kuba'> . As requested by OP, for python 2.4

print("<input type='text' name='%s'>"%name1)

Similarly you could use other concatination option like + as pointed by @Rafalsonn in the comment.

user2906838
  • 1,178
  • 9
  • 20