Python Forum
concatenating strings with + operator
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
concatenating strings with + operator
#1
Hello,

The purpose of this post is to request either verification or correction of python programming rules for concatenating strings using a + operator. I have been unable to find documentation on how the + operator works.

I found the + operator mentioned in a brief blog explanation on how to connect strings together in python. The explanation included code sample where a + operator was used to connect two smaller strings into a larger string. I ran into a problem when I tried following the example. the problem seemed to be resolved when i changed
dittle="2010-01-07 06:56:00+00:00" most_recent = dittle[:10]+"T"+dittle[:12-6]
The only thing I got from that was error messages saying I was using space and indents wrong.

The problem seemed to be eliminated by replacing inserting space bvefore and after the + operators
dittle="2010-01-07 06:56:00+00:00" most_recent = dittle[:10] + "T" + dittle[:12-6]
Sanity check is: does python require spaces on both sides of the + operator, when + is concatenating strings?
string_a = "abc" string_b = "xyz"
To concatenate those two,
concat_string = string_a + string_b
is ok, but
concat_string = string_a+ string_b concat_string = string_a +string_b concat_string = string_a+ tring_b
All those will fail?

Is a python programming manual where I could find this rule explained?

Any information you can provide will be appreciated immensely.
deanhystad write Aug-09-2025, 01:21 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
All your examples run fine for me, except this:
concat_string = string_a+ tring_b
And that one failed because there is no tring_b.

What version of python are you using? How do you run your programs?
Reply
#3
The white space characters around the + operator are irrelevant to the Python interpreter. Write as many as you want, or none of them, the result is the same.
« We can solve any problem by introducing an extra level of indirection »
Reply
#4
As space characters around the + operator are irrelevant,but can soon get ugly if try to add spaces in string.
Then f-string will make cleaner.
greeting = "Hello " + first_name + " " + last_name + "!"
With f-string,both have same output.
greeting = f"Hello {first_name} {last_name}!"
Your code with f-string
>>> most_recent = dittle[:10] + "T" + dittle[:12-6] >>> most_recent '2010-01-07T2010-0' >>> >>> most_recent = f"{dittle[:10]}T{dittle[:12-6]}" >>> most_recent '2010-01-07T2010-0'
Reply
#5
The spaces around the plus operator are a python standard, but are not required by the interpreter itself. You seem to be using a system that is running a style check before attempting to run the code itself. VSCode, PyCharm, and other IDEs are often set up this way.

Python standards for code formatting are laid out in PEP 8.

There is also the Black style formatter which will take your code and rearrange it so that it matches what it thinks is a good format. The format it targets is PEP 8 compliant but is even stricter. For example Python lets you use single quotes or double quotes around a string. Black wants only double quotes. The target style information is a good guide to writing readably formatted python even if you don't use Black.
Reply
#6
Also you will have a problem with
dittle[:12-6]
. The 12-6 will be evaluated as 6 resulting in
dittle[:6]
which just gives you the first six characters of dittle. I think you may have intended something like
dittle[-6:]
which returns the last six characters of your string.
Reply
#7
Hi,

generally speaking: to concatenate strings in Python with + is considered bad practice, as Python has better ways to it. Either use f-Stings for the format method of strings. Both is extensively covered in the Python documentation.

For the given problem, it is pointless anyway. You better use Python's datetime module:

>>> some_time = '2010-01-07 06:56:00+00:00' >>> from datetime import datetime >>> datetime.fromisoformat(some_time) datetime.datetime(2010, 1, 7, 6, 56, tzinfo=datetime.timezone.utc) >>> datetime.fromisoformat(some_time).isoformat() '2010-01-07T06:56:00+00:00' >>>
Regards, noisefloor
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to understand strings and lists of strings Konstantin23 2 2,371 Aug-06-2023, 11:42 AM
Last Post: deanhystad
  Splitting strings in list of strings jesse68 3 3,214 Mar-02-2022, 05:15 PM
Last Post: DeaD_EyE
  concatenating 2 items at a time in a python list K11 3 4,106 Oct-21-2020, 09:34 AM
Last Post: buran
  concatenating lists in a comprehension Skaperen 3 3,495 Jan-01-2020, 08:10 PM
Last Post: ichabod801
  Concatenating strings in cgi Meera2019 2 3,012 Aug-31-2019, 05:57 PM
Last Post: Meera2019
  Finding multiple strings between the two same strings Slither 1 3,749 Jun-05-2019, 09:02 PM
Last Post: Yoriz
  concatenating list with tuple Skaperen 7 7,196 Sep-28-2018, 05:55 AM
Last Post: Skaperen
  lists, strings, and byte strings Skaperen 2 5,647 Mar-02-2018, 02:12 AM
Last Post: Skaperen
  How to create a long string by concatenating two lists nikhilkumar 11 10,685 Jul-12-2017, 05:36 PM
Last Post: wavic

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.