Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Note added on string concatenation #261

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 14, 2013
Merged

Note added on string concatenation #261

merged 2 commits into from
Apr 14, 2013

Conversation

ozgur
Copy link
Contributor

@ozgur ozgur commented Mar 31, 2013

Besides the join() function and the + operator for concatenation , there is also this % formatting operator to merge the pre-determined number of strings. However, What's new in Python 3 tells that this will be replaced by the format() function in the later versions.

I added a small note about this change to keep the guys informed.

**%** operator became deprecated in Python 3.1 and will be replaced by the
**format()** method in the later versions.

.. code-block:: python
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A newline is needed after this code-block line (otherwise the foo and bar assignments are treated as arguments to the code-block, and sphinx fails to build this entire code-block).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the warning. I fixed it.

@rgbkrk
Copy link
Contributor

rgbkrk commented Mar 31, 2013

Not sure what others think, but I prefer to push people toward the right path for the future, which is to just use .format(...) (at least as far back as 2.6).

@MaPePeR
Copy link

MaPePeR commented Apr 1, 2013

But when you see the % Operator in the wild you should already have heard about it before, so i think it's worth mentioning.

@simon-weber
Copy link
Contributor

I think it might be clearer to make a note of formatting techniques after the concatenation section, not within it. Otherwise, it seems like you're implying + and % are equally as good for the third example:

foo = 'foo'
bar = 'bar'

foobar = foo + bar  # This is good
foobar = "%s%s" % (foo, bar)  # This is good? I don't agree =)

Formatting is meant for, well, formatting - not concatenation. Maybe a final paragraph like:
"If you're trying to fill in a template or format your strings for output, use .format: [quick example]".

@ozgur
Copy link
Contributor Author

ozgur commented Apr 8, 2013

Actually, I didn't say in the note that formatting and + approach are equally good, besides formatting with % is slower than the + operator if you concatenate two strings. Here, I just pointed out the role of formatting within the concept of string concatenation. If someone gives a fully-detailed explanation about string formatting separately, it can be done in a different section and maybe linked with this note as well.

PS: Anyone keen on writing the string formatting section, should in my humble opinion discourage the use of % operator whatsoever.

kennethreitz pushed a commit that referenced this pull request Apr 14, 2013
Note added on string concatenation
@kennethreitz kennethreitz merged commit fee9fcd into realpython:master Apr 14, 2013
@kennethreitz
Copy link
Contributor

🍰

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants