473,499 Members | 1,593 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Re: string concatenate

On Wed, 01 Oct 2008 09:41:57 -0700, sandric ionut wrote:
Hi:

I have the following situation:
Â*Â*Â* nameAll = []
Here you defined nameAll as a list
Â*Â*Â* for i in range(1,10,1):
That range is superfluous, you could write this instead[1]:
for i in range(10):
Â*Â*Â*Â*Â*Â*Â* n = "name" + str([i])
in this, you're converting a list into a string. If i was 2, the
conversion result into: 'name' + '[2]'
Â*Â*Â*Â*Â*Â*Â* nameAll += n
Here you're appending n into the list nameAll. Python's string behaves
like a list, that it is iterable, so list accepts it.
Â*Â*Â* print nameAll
your code should be:
listAll = []
for i in range(1, 11):
n = "name" + str(i)
listAll.append(n)
print ' '.join(listAll)

or using list comprehension and string interpolation:
print ' '.join('name%s' % i for i in range(1, 11))

[1] Note that range(10) starts with 0, and produces a list of 10 numbers.
If, like in your expected result, you want name1 till name10, you'll need
range(1, 11) because range is half-open, it includes 1, but not 11. This
behavior has some unique property that simplifies many things, although
it do takes some time to get used to.

Oct 1 '08 #1
0 743

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
3614
by: Jonas Galvez | last post by:
Is it true that joining the string elements of a list is faster than concatenating them via the '+' operator? "".join() vs 'a'+'b'+'c' If so, can anyone explain why?
35
2407
by: michael.casey | last post by:
The purpose of this post is to obtain the communities opinion of the usefulness, efficiency, and most importantly the correctness of this small piece of code. I thank everyone in advance for your...
2
3137
by: Alessandro Rossi | last post by:
Hi, I have this problem: In C# i have to concatenate a string "hi Carl" with another string like this """, but C# gives me an error. How can i concatenate a string with the " character? Thank...
3
8789
by: Tom | last post by:
Hi I want to retrieve database data and concatenate them to a string. Here is my code .ToString()); //runtime error her } while (dr.NextResult()) string array =...
5
3047
by: SDL20 | last post by:
How do I incement a numeric portion in Access 2003? The field I want to numerically increment looks like this: Emp-1 Emp-2 .... .... Emp-n
5
2791
by: Generic Usenet Account | last post by:
I have been to recreate a problem that I am having with strings with the trivial code snippet given below. In the trivial code example, I am reading five lines from a data file, each line having...
6
5777
by: Registered User | last post by:
Hi experts, I'm trying to write a program to solve the following exercise: Accept three strings from the user. Find the first occurrence of the first string in the third string. If it is present...
3
8019
by: =?iso-8859-1?B?Qmr2cm4gS2VpbA==?= | last post by:
Hello pythons, I have little problem with understanding conversions in python. I've written a little class - nothing much, just to try out Python a little - containing the following method: ...
7
8260
by: largedimples | last post by:
The assignment was as follows: A character string can be implemented as a linked list of characters. Implement a C++ ADT called Newstring that uses linked lists to implement the following string...
13
21779
by: sinbad | last post by:
hi, how to concatenate a "hash defined" constant value to another "hash defined" constant string. For example #define ABC 100 #define MYSTR "The value of ABC is" Now i need a string that...
0
7132
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
6899
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5475
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4919
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4602
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3103
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3094
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1427
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
302
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.