Python concat list with a string variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Python concat list with a string variable
# 1  
Old 03-01-2017
Python concat list with a string variable

Hello!

Code:
#!/usr/bin/env python
hn = "simsa-svr"
    for container in containerslist:
        Name = container['Names']

I want to print Name along with hn i,e simsa-svr. I tried like Name = container['Names']'-'.join(hn) did not work.

Needed result:
Code:
lilly2232321-simsa-svr

# 2  
Old 03-01-2017
You are using join wrong in the above example. Join needs a list or tuple of strings:
Code:
>>> '-'.join(('north','east','south','west'))
'north-east-south-west'

Alternatively use the add (+) operator:
Code:
>>> 'Grace' + 'Kelly'
'GraceKelly'

Andrew
This User Gave Thanks to apmcd47 For This Post:
# 3  
Old 03-01-2017
Code:
Name = str(container['Names'])

print str(Name[0])+'-'+hn

But it does print extra / in front of
Code:
/lilly2232321-simsa-svr

How to get rid of it?

Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!


---------- Post updated at 08:13 PM ---------- Previous update was at 07:06 AM ----------

Just used this along with Name

Code:
.replace("/","")+

It works.

Last edited by ashokvpp; 03-01-2017 at 09:01 AM.. Reason: Added CODE tags.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Python variable at different position

I have a file which records banking transactions say like below. user1 has deposited 10,000$ in his account user2 has deposited 11,000$ in his account user1 has withdraw today 5000$ from his account. Lets say i read this file and convert each line as a list. username= word action=... (1 Reply)
Discussion started by: sahil_shine
1 Replies

2. UNIX for Beginners Questions & Answers

Check if string variable is a subset of another string variable

Below is my ksh shell script where I need to check if variable fileprops is a subset of $1 argument. echo "FILE PROPERTY: $fileprops" echo "PARAMETER3: $1" if ; then echo "We are Good. $line FILE is found to be INTACT !! " else echo... (2 Replies)
Discussion started by: mohtashims
2 Replies

3. Shell Programming and Scripting

Concat String with variable after a 'grep' and awk

Here is the structure of my file: MyFile.txt g-4.n.g.fr 10.147.243.63 g-4.n.g.fr-w1 Here is my sript: test.sh #! /bin/sh ip=10.147.243.63 worker=$(grep -e $ip $1 | awk '{ print $3; }') echo "" echo $worker echo "" echo $worker echo "" echo "$worker.v.1" echo... (7 Replies)
Discussion started by: chercheur111
7 Replies

4. Shell Programming and Scripting

Variable Substitution in Python

Hi, Please I have the following python code. x = time.strftime("%Y_%m_%d") os.system("/gsn/mme/parse_ebm_log.pl -x /gsn/mme/ -u -r gsm -f /gsn/mme/mme01/ebs/A* >> /gsn/mme/mme01/+ str(x)_gsm.txt") os.system("/gsn/mme/parse_ebm_log.pl -x /gsn/mme/ -u -r wcdma -f /gsn/mme/mme01/ebs/A* >>... (2 Replies)
Discussion started by: infinitydon
2 Replies

5. Shell Programming and Scripting

Python get the expect value from a variable

the value of the variable is yes group=bsp_16 keyword="82599" test_var="-a xxx -b yyy" I want to get output with -a xxx -b yyy (0 Replies)
Discussion started by: yanglei_fage
0 Replies

6. Shell Programming and Scripting

Remove lines between the start string and end string including start and end string Python

Hi, I am trying to remove lines once a string is found till another string is found including the start string and end string. I want to basically grab all the lines starting with color (closing bracket). PS: The line after the closing bracket for color could be anything (currently 'more').... (1 Reply)
Discussion started by: Dabheeruz
1 Replies

7. Shell Programming and Scripting

[python]string to list conversion

I have a file command.txt. It's content are as follows:- The content of file is actually a command with script name and respective arguments. arg1 and arg2 are dummy arguments , format : -arg arg_value test is a argument specifying run mode , format : -arg In my python code, i read it and... (1 Reply)
Discussion started by: animesharma
1 Replies

8. UNIX for Dummies Questions & Answers

String concat that keeps quotes

Hi All, I hope you can help. i am concatenating String variables using the following method. command="$command$x" i have created a script which takes a set of args passed to the script using the $* #example script args=$* count=0 for x in $args do count=`expr $count + 1` ... (8 Replies)
Discussion started by: duke
8 Replies

9. Programming

simple question on string concat

This is a simple question... char *str = NULL; int num = 0; scanf ("%d", &num); str = ??? I want str to point to the string num.txt For e.g: If user enters num = 5, str should point to "5.txt" How do I do that ? Thanks (2 Replies)
Discussion started by: the_learner
2 Replies

10. Shell Programming and Scripting

concat string

hey, I want to concat whole bunch of strings together but somehow they don't turn out the way I want them to a="HELLO " b="WORLD " c=$a$b I was expecting c to be "HELLO WORLD " but it... (1 Reply)
Discussion started by: mpang_
1 Replies
Login or Register to Ask a Question