Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Python 

Proper String Formatting

Jump into 3.6 now im fan of f-strings right now, huh wat is wrong with you !
(Feb 21, 2017 01:02 PM)SIM-1866423 Wrote:  Jump into 3.6 now im fan of f-strings right now, huh wat is wrong with you !

Yeah, f-strings look really cool but most of my clients are on 3.5 so anything that I update needs to be compatible.
I agree. I'm pretty well known on here for being a Python 3 fuckboi, and 3.6 did some great things with asyncio, but when it comes down to writing modules that you want people to use, then writing it for 3.3+ is the best way to go (or at least 3.5 which has been around for a couple of years almost).
SRPP Discord Server

I am a professional network/cloud security architect.
My Old Tutorials
IMO i personally like the commas and + over format(). Just to me it looks so much easier to understand and makes more sense, however I am always open minded if someone cared to explain to me why .format() is better.
(Feb 21, 2017 08:24 PM)mattdax Wrote:  IMO i personally like the commas and + over format(). Just to me it looks so much easier to understand and makes more sense, however I am always open minded if someone cared to explain to me why .format() is better.

Not every object can be concatenated together. If you look at my first example, try doing:

Code
first_name = "Ron"
last_name = "Paul"
age = 5
print("Your name is " + first_name + " " + last_name + " and your age is " + age)

This one wont error.
Code
print("Your name is {0} {1} and your age is {2}.".format(first_name, last_name, age))
(Feb 21, 2017 08:27 PM)(╯°□°)╯︵ ┻━━┻ Wrote:  Not every object can be concatenated together. If you look at my first example, try doing:

Code
first_name = "Ron"
last_name = "Paul"
age = 5
print("Your name is " + first_name + " " + last_name + " and your age is " + age)

This one wont error.
Code
print("Your name is {0} {1} and your age is {2}.".format(first_name, last_name, age))

Right, but in that case you could just use commas and you wouldn't need worry about casting.
(Feb 21, 2017 08:49 PM)mattdax Wrote:  Right, but in that case you could just use commas and you wouldn't need worry about casting.

Can you map a dictionary with commas? Or a class?

Honestly one of the best features of format().
(Feb 21, 2017 06:51 PM)Goodies Wrote:  I agree. I'm pretty well known on here for being a Python 3 fuckboi, and 3.6 did some great things with asyncio, but when it comes down to writing modules that you want people to use, then writing it for 3.3+ is the best way to go (or at least 3.5 which has been around for a couple of years almost).

Nothing wrong with being a python 3 fuckboi, in basic terms, its literally the future of python.
Thanks for that just starting out, this helped a lot
(Mar 1, 2017 08:24 PM)IChoseDogLeading Wrote:  Thanks for that just starting out, this helped a lot

No problem! Anything else you think that would be helpful?

I'm currently working on a semi-intermediate subject but I'm trying to explain it in a way that anybody should be able to understand.
I've updated it a little bit:

http://pythonforalayman.com/string-formatting


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C] by lo0srMar 22, 2020 02:02 PM 3 129 Mar 22, 2020 02:02 PM
Last Post: HFDone
[Python] by roosiinfidel09Jan 13, 2020 08:33 AM 7 207 Jan 13, 2020 08:33 AM
Last Post: roosiinfidel09
[C] by MordiciaNov 15, 2019 07:21 PM 3 211 Nov 15, 2019 07:21 PM
Last Post: Mordicia