LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 12-27-2017, 01:17 PM   #1
killingthemonkey
Member
 
Registered: Mar 2011
Location: Winston-Salem, NC
Distribution: Fedora, CentOS, Linux Mint
Posts: 144

Rep: Reputation: 16
Python - Concatenating a String With a Variable


Code:
listOfStrings = [ "Hello"
                  "World"
                  "Where"
                  "have"
		  "you"
		  "been"]
						  
for i in range(len(listOfStrings)):
    print(listOfStrings[i].upper() + "\n")
My output:
Code:
HELLOWORLDWHEREHAVEYOUBEEN
Can someone please tell me why the newline isn't being output?

Thank you for your help.
 
Old 12-27-2017, 01:35 PM   #2
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: Slackware
Posts: 7,808

Rep: Reputation: 3047Reputation: 3047Reputation: 3047Reputation: 3047Reputation: 3047Reputation: 3047Reputation: 3047Reputation: 3047Reputation: 3047Reputation: 3047Reputation: 3047
Because Python, like C, automatically concatentates strings that are separated only by whitespace.

For a list of strings, you need commas.

Code:
["a", "b", "c"]
 
Old 12-27-2017, 01:41 PM   #3
killingthemonkey
Member
 
Registered: Mar 2011
Location: Winston-Salem, NC
Distribution: Fedora, CentOS, Linux Mint
Posts: 144

Original Poster
Rep: Reputation: 16
dugan,
Thank you! If I'm not mistaken, the book I'm working through advised me that they needed to be separated by commas. Should have been blatantly obvious. Actually, I guess it was to you. To me? Not so much.
 
Old 12-28-2017, 12:21 PM   #4
Sefyir
Member
 
Registered: Mar 2015
Distribution: Linux Mint
Posts: 542

Rep: Reputation: 241Reputation: 241Reputation: 241
Quote:
Originally Posted by killingthemonkey View Post
Code:
for i in range(len(listOfStrings)):
    print(listOfStrings[i].upper() + "\n")
Also, the range / len / indexing isn't needed

Code:
listOfStrings = [ "Hello",
                   "World",                   
                   "Where",
                   "have",
                   "you",
                   "been"]
Code:
for i in range(len(listOfStrings)):
    print(listOfStrings[i].upper() + "\n")
Code:
 for i in listOfStrings:
   print(i.upper() + '\n')
 
Old 12-28-2017, 12:35 PM   #5
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 10,091

Rep: Reputation: 2986Reputation: 2986Reputation: 2986Reputation: 2986Reputation: 2986Reputation: 2986Reputation: 2986Reputation: 2986Reputation: 2986Reputation: 2986Reputation: 2986
Code:
print "\n".join(listOfStrings).upper()
 
  


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
Issue String concatenating in output file daredevil_1981 Linux - Newbie 4 10-14-2011 01:56 PM
[SOLVED] Bash concatenating string to variable abercrombieande Programming 4 01-19-2011 08:04 AM
[SOLVED] Concatenating an integer to a string mohabic Programming 5 10-19-2010 02:34 AM
Help: removing a variable substring from a string variable in sh script gnparsons Programming 2 06-04-2008 06:21 PM
Problem concatenating a string in perl Singist Programming 4 03-22-2006 07:15 PM

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

All times are GMT -5. The time now is 12:59 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration