Want to combine strings but NOT concatenate them

Tell us what’s happening:
Describe your issue in detail here.

I have two strings that I want to write adjacent to one another. I do not want to concatenate them into one string, I want two strings sitting next to each other. I believe they produce the same result, but my code is failing tests because of this slight difference.

Your code so far

If
var1=‘a’
and
var2 = ‘b’
I want a result of
‘a’ ‘b’
or
‘a’
‘b’
using the variables var1 and var2. I am uninterested in getting the result ‘ab’.

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36

Challenge: Arithmetic Formatter

Link to the challenge:

Can you describe more the thing you are trying to make? The only way to combine two strings into one string is with concatenation. Do you want an array or list of strings?

Thank you Jeremy! Let me know if this helps make it clear:
an example output from my program is
’ 3801 123 \n- 2 + 49 \n------ ----- '.

I am failing the test as the expected answer is
’ 3801 123\n’
‘- 2 + 49\n’
‘------ -----’
These of course return the exact same output, but are slightly different.

You can use a print() statement for each,
print(‘3801 123’)
print(’- 2 + 49’)
print(’------ -----’, end=’’)
or one print statement;
print(‘3801 123’, ‘- 2 + 49’, ‘------ -----’, sep=’\n’, end=’’)

Indeed these give the same result when printed, but my code fails the tests unless I can get an output that is three adjacent strings as written above, not one long string. I just don’t see anybody writing multiple strings adjacent like that anywhere.

A simpler version:

If var1 = ‘a’ and var2=‘b’, i need some operation (or function) given by * such that
var1*var2 = ‘a’ ‘b’
instead of ‘ab’.

I think you might be misinterpreting the error message. Can you post the message in full please?

The thing that you are describing doesn’t exist.

Haha yes this is what I was thinking. Trying to figure out what my real problem is. Here is the error message. arithmetic_arranger() is the function I’m working with is

E       AssertionError: Expected different output when calling "arithmetic_arranger()" with ["3801 - 2", "123 + 49"]
E       assert '  3801      ...    -----    ' == '  3801      ...----    -----'
E         -   3801      123
E         +   3801      123    
E         ?                ++++
E         - -    2    +  49
E         + -    2    +  49    
E         ?                ++++
E         - ------    -----...
E         
E         ...Full output truncated (3 lines hidden), use '-vv' to show

I don’t what “use ‘-vv’ to show” is but it would probably help

I just reviewed the challenge a bit. You need to justify each corresponding solution so that they line up appropriately. You’re issue isn’t with concatenation, but your overall output. The spacing between each equation must also be uniform.
It would probably be more helpful if you provided a link to your replit project for this solution.
In my answer I built a string for each section (top, bottom, dashes, answer) to meet the requirements, using f-strings with width formatting etc.

Those + marks indicate something is there that the solution did not expect. But it looks like nothing is there… Which probably means that you are adding extra whitespace at the end of each line before concatenating.

And yea, the link to your code would be helpful. You have an XY situation here where you asked about possible solution X instead of observed problem Y. Common/classic oops.

1 Like

Yeah that’s the approach I took. I added the space in a pretty janky way, counting the number of characters in the larger integer and then calculating how many spaces would be needed for each equation. I haven’t had much experience with strings, so if there’s a much more elegant/easier way then I will have to look into that.

Now I know to review the test code I’ll be using before I start writing my own program!

I’m not sure my replit link has worked

Sometimes the live preview gets screwy. You can disable the preview by putting < > around the link.

https://replit.com/@KyleSchneider2/boilerplate-arithmetic-formatter-1#arithmetic_arranger.py

ty, sorry if it’s hard to read or something, I have taken a total of zero coding classes in my life

If you look at line 95, you always add 4 spaces, but you shouldn’t for the last problem. Those are the four + in the error message.

Also, you need to return the error messages, not print them.

2 Likes

Ah okay I didn’t realize that would cause an issue, thanks. Those fixes brought down from 10 failed to 6 failed and I bet the rest are similar spacing issues.

You can format a string using f-strings so that it will have a specific width and can be justified (right, left, center), amongst other properties. For this project you can find the width of each problem in the list, by getting the max of the length from the 2 operands (top, bottom) and then add 2 for the operator and the space between the operator and the bottom operand. Setting this value to a variable called width, and using the right justify format specifier ‘>’ would look like;
f"{operand:>{width}d}"
You can use the sign format specifier ‘+’ to provide the positive or negative notation for a number. By default, I believe, this will also provide a right justify. So using this would look like;
Edit for clarification: ‘+’ will provide a sign for both positive and negative numbers, and it doesn’t justify, but will fill the width with the sign being leftmost and the number being rightmost, using the padding character (default is a space ’ ') to fill between.

f"{operand:+{width}d}"
The trailing ‘d’ here just means that ‘operand’ is a decimal (base 2) type number.

Here’s some additional info to help with learning string formatting and format specifiers.

In the error message when it says “assert X==Y”, is the expectation X or Y?

Awesome I think this is a much better way to get it done thank you

1 Like

You can check in the test suite. One side should say actual and the other side should say expected. Expected is the desired result.

1 Like

Generally depends on the order the arguments are written in the assertion-function.
Though looking at your error message, the “+” message has trailing spaces, so that’s your output - which the forum would actually auto-format if you remove everything before the symbols.

-   3801      123
+   3801      123    
?                ++++
2 Likes