1

I am facing a problem while calling the variable values within the double quote

Here's my code:

AccountID = ["1234567","5678912"]
for account in range(len(AccountID)):
    response = sts.assume_role(RoleArn=(f"arn:aws:iam::{AccountID[account]}:role/Cloudbees_Jenkins"), RoleSessionName="learnaws-test-session")
    print(response)

I have return response output with no variable values

File "test3.py", line 19
    response = (RoleArn=(f"arn:aws:iam::{AccountID[account]}:role/Cloudbees_Jenkins"), RoleSessionName="learnaws-test-session")
                       ^
SyntaxError: invalid syntax

how would i return an expected results like:

arn:aws:iam::1234567:role/Cloudbees_Jenkins
arn:aws:iam::5678912:role/Cloudbees_Jenkins
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
shailysharma
  • 61
  • 1
  • 5
  • 1
    Do you get what you want if you write `f'arn:aws:iam::{AccountID[account]}:role/Cloudbees_Jenkins'`? (note the "f" and the braces) – The Photon Dec 31 '22 at 04:52
  • Adding onto what @ThePhoton said, you probably need to not loop over `range` as well – CoffeeTableEspresso Dec 31 '22 at 05:06
  • Does this answer your question? [Is there a Python equivalent to Ruby's string interpolation?](https://stackoverflow.com/questions/4450592/is-there-a-python-equivalent-to-rubys-string-interpolation) – CoffeeTableEspresso Dec 31 '22 at 05:08
  • You've changed the example quite a bit, invalidating current answers. Now I have to delete mine. And the line in the error traceback doesn't match the line in the script. The script doesn't have a syntax error. You seem to be running multiple versions of the code. Run the script posted and the traceback error will go away (or maybe change to the next error). – tdelaney Dec 31 '22 at 05:22

3 Answers3

0

You could use f-strings, a very convenient way to refer to variables inside print statements in python.

In your case, it would look like this:

response = f"arn:aws:iam::{int(AccountID[0])}:role/Cloudbees_Jenkins"
testfire10
  • 13
  • 2
0
response = (RoleArn=(f"arn:aws:iam::{AccountID[account]}:role/Cloudbees_Jenkins"), RoleSessionName="learnaws-test-session")

The problem here has nothing to do with generating the string.

The problem is that in Python the = (assignment) operator does not produce an output value.

So the expression RoleArn = ...a_bunch_of_stuff... only assigns that stuff to RoleArn. It doesn't produce any output value that can be assigned to response.

And the assignement RoleSessionName="learnaws-test-session" doesn't produce any return value that can be assigned to be part of the tuple that is being assigned to RoleArn.

So just break the code up into three lines:

RoleSessionName="learnaws-test-session"
RoleArn= f"arn:aws:iam::{AccountID[account]}:role/Cloudbees_Jenkins"
response = (RoleArn, RoleSessionName)

In recent Python versions (3.8 and newer) you can use the "walrus operator" := to both assign a value and return that value to be used in an expression, but in your code you should not because it IMO makes the code less clear.

The Photon
  • 1,242
  • 1
  • 9
  • 12
  • OP seems to have messed up a run of the code. The posted scripted doesn't have this mangled line. – tdelaney Dec 31 '22 at 05:23
  • @tdelaney, Both of the code blocks posted by OP try to use `=` on the r.h.s. of another `=` statement. – The Photon Dec 31 '22 at 05:25
  • The = on the rhs is part of a keyword parameter assignment and is fine. You could run that code and you would not get a syntax error. When I ran it I got "NameError: name 'sts' is not defined. Did you mean: 'str'?" – tdelaney Dec 31 '22 at 05:29
0
AccountID = [1234567,5678912]
for i in AccountID:
    print("arn:aws:iam::",i,"role/Cloudbees_Jenkins")
Julia Meshcheryakova
  • 3,162
  • 3
  • 22
  • 42
  • Your answer could be improved by adding more information on what the code does and how it helps the OP. – Tyler2P Dec 31 '22 at 10:39