How to use a variable as part of a command?

Asked

Viewed 41 times

-1

How can I use the value of a variable as part of a command?

Example

count = 1
tn.write(b'ip address 10.0.0.count 0\n')

I would like the value 1 of the variable count is the last digit of the IP address in the command.

  • Helder, translate your question, please.

  • Welcome to the Portuguese OS. Here we speak in en, please translate your question and do not forget to do the [tour] to understand how the community works.

1 answer

1

Helder, I believe you want to replace the value present in the Count variable in the string, there are several ways to do this in Python, below some examples:

count = 1

print(f'ip address 10.0.0.{count} 0\n')
print('ip address 10.0.0.' + str(count) + ' 0\n')
print('ip address 10.0.0.%s 0\n'%count)
print('ip address 10.0.0.{} 0\n'.format(count))
  • Thank you very much, I’ll test it now...

  • And I apologize for posting in English...

  • I try the following solution: tn.write(b'ip address 10.0.0.%s 0\n'%count) but he’s wrong, says Typeerror: %b requires a bytes-like Object, or an Object that Implements bytes, not 'int'' , but I don’t understand after all I have the b making Encode for bytes..

Browser other questions tagged

You are not signed in. Login or sign up in order to post.