473,473 Members | 1,951 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Concatenation

6 New Member
After getting raw_input from the user, I want to print a sentence that ends in an integer and then the "." character. Of course, if I print using a comma (,) I don't get an error but there is an ugly space between the integer and the period. But if I try "+" then I obviously get a str/int concatenation error.

One way to solve this is to convert the int into a str and then "+" works. But is there an easier way without having to call the str() method?
Feb 19 '08 #1
8 3881
numberwhun
3,509 Recognized Expert Moderator Specialist
After getting raw_input from the user, I want to print a sentence that ends in an integer and then the "." character. Of course, if I print using a comma (,) I don't get an error but there is an ugly space between the integer and the period. But if I try "+" then I obviously get a str/int concatenation error.

One way to solve this is to convert the int into a str and then "+" works. But is there an easier way without having to call the str() method?
Um, what language are you working in?
Feb 19 '08 #2
pyn00b
6 New Member
Um, what language are you working in?
Python. So here is some code...

num = raw_input("Pick a number: ")
num = int(num)
print "Your number times 5 is", num * 5, "."

This produces a space between num and the period. If I try to use "+" I get an error trying to concatenate a str and int. It's possible to print str(num) and then concatenate that result with the period to eliminate the space. But I was wondering if there was a more obvious way than calling the str() function.
Feb 19 '08 #3
Ganon11
3,652 Recognized Expert Specialist
Moved the thread to the Python forum.
Feb 19 '08 #4
pyn00b
6 New Member
Moved the thread to the Python forum.
Thanks! Sorry about that. New here. :)
Feb 19 '08 #5
dshimer
136 Recognized Expert New Member
And don't forget Code tags.

My favorite way (because there are several) is using string formatting.
Expand|Select|Wrap|Line Numbers
  1. >>> num = raw_input("Pick a number: ")
  2. >>> num = int(num)
  3. >>> print "Your number times 5 is %d."%(num * 5)
  4. Your number times 5 is 30.
  5.  
You could also move the int() call to tighten the code a little.
Expand|Select|Wrap|Line Numbers
  1. >>> num = int(raw_input("Pick a number: "))
  2. >>> print "Your number times 5 is %d."%(num * 5)
  3. Your number times 5 is 40.
  4.  
or for that matter
Expand|Select|Wrap|Line Numbers
  1. print "Your number times 5 is %d."%(int(raw_input("Pick a number: ")) * 5)
Once you start worrying about what happens if the user enters a non numeric value like "a" you will want to explore the try: and except: block.
Feb 19 '08 #6
jlm699
314 Contributor
Expand|Select|Wrap|Line Numbers
  1. num = raw_input("Pick a number: ")
  2. num = int(num)
  3. print "Your number times 5 is", num * 5, "."
  4.  
This produces a space between num and the period. If I try to use "+" I get an error trying to concatenate a str and int. It's possible to print str(num) and then concatenate that result with the period to eliminate the space. But I was wondering if there was a more obvious way than calling the str() function.
So why not use the str() conversion? It's probably just as easy...

Expand|Select|Wrap|Line Numbers
  1. num = int(raw_input("Pick a number: "))
  2. print "Your number times 5 is " + str(num * 5) + "."
  3.  
I guess it's a matter of preference, but that's how I do all my string/int concatenations...
Feb 19 '08 #7
bvdet
2,851 Recognized Expert Moderator Specialist
I try to avoid string concatenation where possible. String formating with %s uses str() to generate the string.
Expand|Select|Wrap|Line Numbers
  1. num = int(raw_input("Pick a number: "))
  2. print "Your number times 5 is %s." % (num * 5)
Feb 19 '08 #8
pyn00b
6 New Member
dshimer, jlm699, and bvdet - thanks to you all for responding. That was very helpful and educational!
Feb 19 '08 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Oliver Crow | last post by:
As a realtive python newb, but an old hack in general, I've been interested in the impact of having string objects (and other primitives) be immutable. It seems to me that string concatenation is...
5
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?
1
by: Fahd Khan | last post by:
Hi team! While troubleshooting a crash I had while using BitTorrent where the torrent's target file names didn't fall into the ascii range I was playing around in the interpreter and noticed this...
1
by: G Kannan | last post by:
Hey all! I have written a perl script to retrieve information from a HTML Form and insert the data into an Oracle database table. I am gettting the the following error message: "Use of...
7
by: Paul Davis | last post by:
I'd like to overload 'comma' to define a concatenation operator for integer-like classes. I've got some first ideas, but I'd appreciate a sanity check. The concatenation operator needs to so...
8
by: mrstephengross | last post by:
I'm using gcc 3.3.1 to compile the following code (below). I've written a macro to simplify writing operators. The macro uses the '##' operator to paste together 'operator' and the name of the...
9
by: Justin M. Keyes | last post by:
Hi, Please read carefully before assuming that this is the same old question about string concatenation in C#! It is well-known that the following concatenation produces multiple immutable...
33
by: genc_ymeri | last post by:
Hi over there, Propably this subject is discussed over and over several times. I did google it too but I was a little bit surprised what I read on internet when it comes 'when to use what'. Most...
34
by: Larry Hastings | last post by:
This is such a long posting that I've broken it out into sections. Note that while developing this patch I discovered a Subtle Bug in CPython, which I have discussed in its own section below. ...
34
by: raylopez99 | last post by:
StringBuilder better and faster than string for adding many strings. Look at the below. It's amazing how much faster StringBuilder is than string. The last loop below is telling: for adding...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
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
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
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
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 ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
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.