string-concatenation

Is it more Pythonic to use String Formatting over String Concatenation in Python 3?

一曲冷凌霜 提交于 2019-11-28 01:54:04
问题 So I'm programming a text game in Python 3.4 that requires the use of the print() function very often to display variables to the user. The two ways I've always done this is with string formatting and string concatenation : print('{} has {} health left.'.format(player, health)) And, print(player + ' has ' + str(health) + ' health left.') So which is better? They're both equally as readable and quick to type, and perform exactly the same. Which one is more Pythonic and why? Question asked as I

How to collapse a list of characters into a single string in R

蹲街弑〆低调 提交于 2019-11-28 00:37:53
There is a list which I would like to output into an excel file as a single string. I start with a list of characters. url="http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=pubmed&id=21558518&retmode=xml" xml = xmlTreeParse(url,useInternal = T) ns <- getNodeSet(xml, '//PublicationTypeList/PublicationType') types <- sapply(ns, function(x) { xmlValue(x) } ) types Output is this: [1] "Journal Article" "Multicenter Study" "Research Support, N.I.H., Extramural" [4] "Research Support, Non-U.S. Gov't" So in types - there is a list of characters Now I need to make into a single string. This

String.Format vs “string” + “string” or StringBuilder? [duplicate]

依然范特西╮ 提交于 2019-11-27 23:14:52
问题 This question already has answers here : Closed 10 years ago . Possible Duplicates: Is String.Format as efficient as StringBuilder C# String output: format or concat? What is the performance priority and what should be the conditions to prefer each of the following: String.Format("{0}, {1}", city, state); or city + ", " + state; or StringBuilder sb = new StringBuilder(); sb.Append(city); sb.Append(", "); sb.Append(state); sb.ToString(); 回答1: Compiler will optimize as much string concat as it

Is python += string concatenation bad practice?

爷,独闯天下 提交于 2019-11-27 22:56:13
问题 I am reading The Hitchhiker’s Guide to Python and there is a short code snippet foo = 'foo' bar = 'bar' foobar = foo + bar # This is good foo += 'ooo' # This is bad, instead you should do: foo = ''.join([foo, 'ooo']) The author pointed out that ''.join() is not always faster than + , so he is not against using + for string concatenation. But why is foo += 'ooo' bad practice whereas foobar=foo+bar is considered good? is foo += bar good? is foo = foo + 'ooo' good? Before this code snippet, the

String to variable name in R

怎甘沉沦 提交于 2019-11-27 21:49:03
问题 I am cleaning a data set and I need to choose the variables depending on another variable. Let's say that if ID = 1 , I need to introduce in the data frame the variable VAR01 , if ID = 2 , I need VAR02 , and so on. Thus, I'm doing a for loop where I paste the variable name 'VAR' with the ID number with the stringf function. The problem is that I need R to understand the string as a function name. I've found in the forum this solution, which doesn't work for me: > variable1 = c("monday",

How to concatenate variables in Perl

浪尽此生 提交于 2019-11-27 18:36:26
问题 Is there a different way to concatenate variables in Perl? I accidentally wrote the following line of code: print "$linenumber is: \n" . $linenumber; And that resulted in output like: 22 is: 22 I was expecting: $linenumber is: 22 So then I wondered. It must be interpreting the $linenumber in the double quotes as a reference to the variable (how cool!). What are the caveats to using this method and how does this work? 回答1: Variable interpolation occurs when you use double quotes. So, special

Merge two JSON objects programmatically

不羁的心 提交于 2019-11-27 18:34:04
问题 I have two JSON objects here, generated through the Google Search API. The URL's of these objects can be found below. http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=hello%20world&rsz=large http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=hello%20world&rsz=large&start=8 As you can see the first URL returns the first eight results, whilst the second one returns the next eight. Instead of checking these results separately I'd like to programmatically merge them into one

How can I concatenate forloop.counter to a string in my django template

落爺英雄遲暮 提交于 2019-11-27 17:37:00
问题 I am already trying to concatenate like this: {% for choice in choice_dict %} {% if choice =='2' %} {% with "mod"|add:forloop.counter|add:".html" as template %} {% include template %} {% endwith %} {% endif %} {% endfor %} but for some reason I am only getting "mod.html" and not the forloop.counter number. Does anyone have any idea what is going on and what I can do to fix this issue? Thanks alot! 回答1: Your problem is that the forloop.counter is an integer and you are using the add template

Why does a null value appear in string output?

谁说胖子不能爱 提交于 2019-11-27 16:14:15
When I execute the following code the output is "nullHelloWorld". How does Java treat null? import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ class Ideone { public static void main (String[] args) throws java.lang.Exception { String str=null; str+="Hello World"; System.out.println(str); } } You are attempting to concatenate a value to null . This is governed by "String Conversion", which occurs when one operand is a String , and that is covered by the JLS, Section 5.1.11 : Now only reference values need to be

How to store a string var greater than varchar(max)?

风流意气都作罢 提交于 2019-11-27 14:47:38
问题 I'm trying to do this: DECLARE @myVar VARCHAR(MAX) Loop with cursor select @myVar = @myVar + bla bla bla end loop When the loop ends, @myVar is incomplete, containing only 8000 characters. I have tryed to use text, but is not allowed to local vars. What would be a good solution to this case? xml var? I have just looked this posts: How do I pass a string parameter greater than varchar(8000) in SQL Server 2000? Check if concatenating to a varchar(max) will go beyond max allowable characters And