LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-07-2021, 05:47 AM   #1
czezz
Member
 
Registered: Nov 2004
Distribution: Slackware/Solaris
Posts: 889

Rep: Reputation: 41
Python - pass variable as parameter?


In python, how can I pass variable as a parameter eg. in db connector?
In this example im trying set host as what is defined in: dbhost variable but it does not work. I tried also $dbhost , "dbhost" , '(dbhost)'... and more. None works.

Code:
dbhost="10.1.1.19"
print(dbhost)

connection_parameters = "dbname=db_lab user=lab_user password=123 host=(dbhost) port=5432"

Last edited by czezz; 05-07-2021 at 05:49 AM.
 
Old 05-07-2021, 06:16 AM   #2
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 2,321

Rep: Reputation: Disabled
Use curly braces and f before the opening quote (available starting with Python 3.6)
Code:
connection_parameters = f"dbname=db_lab user=lab_user password=123 host={dbhost} port=5432"
See 7.1. Fancier Output Formatting in The Python Tutorial.

Older method is
Code:
connection_parameters = "dbname=db_lab user=lab_user password=123 host={} port=5432".format(dbhost)
Even older method is
Code:
connection_parameters = "dbname=db_lab user=lab_user password=123 host=%s port=5432" % dbhost

Last edited by shruggy; 05-07-2021 at 06:33 AM.
 
Old 05-07-2021, 06:32 AM   #3
czezz
Member
 
Registered: Nov 2004
Distribution: Slackware/Solaris
Posts: 889

Original Poster
Rep: Reputation: 41
Thanks for reply..

First method does not work:

Code:
    connection_parameters = f"dbname=db_lab user=lab_user password=123 host={dbhost} port=5432"
                                                                                                    ^
SyntaxError: invalid syntax
Second one does, thanks.
However, is there really no other simpler method to substitute static value with variable?
What if I want to substitute all of these values?

Last edited by czezz; 05-07-2021 at 06:39 AM.
 
Old 05-07-2021, 06:45 AM   #4
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 2,321

Rep: Reputation: Disabled
Quote:
Originally Posted by czezz View Post
What if I want to substitute all of these values?
Code:
#!/usr/bin/python3
dbname   = 'db_lab'
user     = 'lab_user'
password = '123'
dbhost   = '10.1.1.19'
port     = 5432

connection_parameters = \
    'dbname={} user={} password={} host={} port={}'.format(
     dbname,   user,   password,   dbhost, port)
print(connection_parameters)
connection_parameters = \
    'dbname=%s user=%s password=%s host=%s port=%d' % \
    (dbname,   user,   password,   dbhost, port)
print(connection_parameters)

# Shell-like substitution is also possible
from string import Template
connection_parameters = \
    Template(
        'dbname=$dbname user=$user password=$password host=$dbhost port=$port')
print(
    connection_parameters.substitute(
        dbname=dbname, user=user, password=password, dbhost=dbhost, port=port)
    )
db = dict(
    dbname   = 'db_lab',
    user     = 'lab_user',
    password = '123',
    dbhost   = '10.1.1.19',
    port     = 5432)
print(connection_parameters.substitute(db))

Last edited by shruggy; 05-07-2021 at 07:21 AM.
 
1 members found this post helpful.
Old 05-07-2021, 06:46 AM   #5
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 16,490

Rep: Reputation: 5532Reputation: 5532Reputation: 5532Reputation: 5532Reputation: 5532Reputation: 5532Reputation: 5532Reputation: 5532Reputation: 5532Reputation: 5532Reputation: 5532
Code:
>>> dbhost="tttttttt"
>>> connection_parameters = f"dbname=db_lab user=lab_user password=123 host={dbhost} port=5432"
>>> print(connection_parameters)
dbname=db_lab user=lab_user password=123 host=tttttttt port=5432
>>>
this one works too, just you made a syntax error somewhere
 
Old 05-07-2021, 06:50 AM   #6
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 2,321

Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
this one works too, just you made a syntax error somewhere
It doesn't work in Python 2 though.
 
Old 05-07-2021, 06:54 AM   #7
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 1,339

Rep: Reputation: 1133Reputation: 1133Reputation: 1133Reputation: 1133Reputation: 1133Reputation: 1133Reputation: 1133Reputation: 1133Reputation: 1133
Quote:
Originally Posted by czezz View Post
Thanks for reply..

First method does not work:

Code:
    connection_parameters = f"dbname=db_lab user=lab_user password=123 host={dbhost} port=5432"
                                                                                                    ^
SyntaxError: invalid syntax
That's the error you get in earlier versions of Python which do not support formatted string literals (aka f-strings).

Shruggy pointed out you need to be on at least Python 3.6 - you haven't stated which version you are running.

(Although from looking at the docs it appears 3.7 has it, but 3.6 doesn't.)

If you are on 3.6, you appear to have until the end of this year before security support ends.


Last edited by boughtonp; 05-07-2021 at 07:01 AM.
 
Old 05-07-2021, 06:58 AM   #8
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 16,490

Rep: Reputation: 5532Reputation: 5532Reputation: 5532Reputation: 5532Reputation: 5532Reputation: 5532Reputation: 5532Reputation: 5532Reputation: 5532Reputation: 5532Reputation: 5532
Quote:
Originally Posted by shruggy View Post
It doesn't work in Python 2 though.
Oh, yes, you are right. Just that was not mentioned anywhere. In that case would be better to use python3 (if possible), because python2 has reached EOL.
 
Old 05-07-2021, 07:06 AM   #9
czezz
Member
 
Registered: Nov 2004
Distribution: Slackware/Solaris
Posts: 889

Original Poster
Rep: Reputation: 41
Thanks guys.
I see switching from bash habits to python will be hard time... well maybe not but its something different
Yes, this specific example was with python 2.7 and yes, i know it should be switched to 3.x.

Thanks!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to pass string variable as command parameter in shell script? prasad1990 Linux - Newbie 6 03-23-2015 12:48 AM
[SOLVED] How can i pass an entire Array into a function parameter by value? (no pointer pass) esgol Programming 26 07-31-2012 11:34 AM
shell getopts: opt w/ optional parameter is taking next opt as its parameter! GrapefruiTgirl Programming 22 10-27-2010 06:00 AM
LXer: Python Python Python (aka Python 3) LXer Syndicated Linux News 0 08-05-2009 08:30 PM
linux bash - how to use a dynamic parameter in shell parameter expansion expression nickleus Linux - General 2 08-21-2006 04:54 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:29 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration