Python Forum
How do I put a variable inside a String in Python?
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do I put a variable inside a String in Python?
#1
hello all ...

im trying to add a variable inside a String ...
this is my code :

import os 
import sys

cmd2 = """powershell -command "Get-Process | where {$_.Description -like 'VMware Tools Core Service'} | select -expand id""" # the commnad run the powerashell and search for a proccess with Description then get her pid number 
tmp = os.popen(cmd2).read()

cmd3 = "wmic process where processId="+tmp+"get name | findstr /v Name"  # this commnad show the name of the pid number thet getting from first command 

tmp2 = os.popen(cmd3).read()
#print("hey " + tmp ) 
print tmp2
the problem here : wmic process where processId="+tmp+"get name | findstr /v Name" ,,,, my script ignore this section of command ( get name | findstr /v Name ) it's add new line and just run the commnad until here : wmic process where processId="+tmp

how i can solve this issue ?

i think the problem is here :
[Image: Untitled.png]

the powershell add a new empty line !! when i try to use len() it gives me 5 while they are just 4 !!
how i can remove the empty line ?
Reply
#2
If I understand correctly, it is adding what you want it to add, but it is also adding a newline. The solution to that would be to change tmp to tmp.strip(), which will remove any leading or trailing white space (like newlines).
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
if python 3.6 or newer, use f-string:
>>> tmp = '12345'
>>> cmd3 = f"wmic process where processId = {tmp} get name | findstr /v Name"
>>> cmd3
'wmic process where processId = 12345 get name | findstr /v Name'
>>>
Reply
#4
(Sep-24-2018, 11:26 AM)Larz60+ Wrote: if python 3.6 or newer, use f-string:
>>> tmp = '12345'
>>> cmd3 = f"wmic process where processId = {tmp} get name | findstr /v Name"
>>> cmd3
'wmic process where processId = 12345 get name | findstr /v Name'
>>>

i use python 2.7 how i can use f"wmic process where processId = {tmp} get name | findstr /v Name" ??
note : "wmic process where processId = {tmp} get name | findstr /v Name" ===> not work in in python 2.7
: "wmic process where processId={}get name | findstr /v Name".format(tmp) ===> work in python 2.7 but ignore ( get name | findstr /v Name )
Reply
#5
(Sep-24-2018, 04:12 PM)evilcode1 Wrote: : "wmic process where processId={}get name | findstr /v Name".format(tmp) ===> work in python 2.7 but ignore ( get name | findstr /v Name )
What does that mean? It works?
Reply
#6
(Sep-24-2018, 04:27 PM)nilamo Wrote:
(Sep-24-2018, 04:12 PM)evilcode1 Wrote: : "wmic process where processId={}get name | findstr /v Name".format(tmp) ===> work in python 2.7 but ignore ( get name | findstr /v Name )
What does that mean? It works?
i mean it takes the variable result from tmp (7128) ... but its run command until this wmic process where processId={}get name
if i use "wmic process where processId={tmp}get name | findstr /v Name" i got nothing just empty space
Reply
#7
any help ?
Reply
#8
What's the current issue? Is string interpolation still not working? Or is the external command pipeline failing?
Reply
#9
(Sep-25-2018, 04:35 PM)nilamo Wrote: What's the current issue? Is string interpolation still not working? Or is the external command pipeline failing?
string interpolation is working ... but python os.popen ignore this section from my commnad : ( get name | findstr /v Name )

it's run commnad until here after getting the pid number for the process from tmp .. its hit enter

wmic process where processId={}

[Image: Untitled.png]

1= the result should be like this
2= the python result

by the way is there anyway to do this in pure python without using powershell and cmd ?? can i get the whole process description with psutil ??

[Image: Windows-_Task-_Manager.png]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Variable definitions inside loop / could be better? gugarciap 2 450 Jan-09-2024, 11:11 PM
Last Post: deanhystad
  How to create a variable only for use inside the scope of a while loop? Radical 10 1,769 Nov-07-2023, 09:49 AM
Last Post: buran
  Replacing String Variable with a new String Name kevv11 2 793 Jul-29-2023, 12:03 PM
Last Post: snippsat
  Need help on how to include single quotes on data of variable string hani_hms 5 2,058 Jan-10-2023, 11:26 AM
Last Post: codinglearner
  python r string for variable mg24 3 2,835 Oct-28-2022, 04:19 AM
Last Post: deanhystad
  USE string data as a variable NAME rokorps 1 970 Sep-30-2022, 01:08 PM
Last Post: deanhystad
  Removing Space between variable and string in Python coder_sw99 6 6,311 Aug-23-2022, 01:15 PM
Last Post: louries
  Remove a space between a string and variable in print sie 5 1,801 Jul-27-2022, 02:36 PM
Last Post: deanhystad
  Split string using variable found in a list japo85 2 1,311 Jul-11-2022, 08:52 AM
Last Post: japo85
  Can you print a string variable to printer hammer 2 1,965 Apr-30-2022, 11:48 PM
Last Post: hammer

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020