Posts: 21 Threads: 5 Joined: Jan 2024 Jan-13-2024, 08:54 PM (This post was last modified: Jan-13-2024, 09:06 PM by johnywhy.) the following tab-delimits each item print ("a", "b") a bIs there an equally simple way to build a tab-delimited string, aside from print? The following creates a tuple, but not a tab-delimited string myString = ("a", "b") print (myString) ('a', 'b')I think i could join with tab char, but that seems clunky and inefficient. I noticed, in my case, i need double-parens so that join sees a single argument. delimited = "\t".join((str(idx), choice, '\n')) A for loop seem even more inefficient. Maybe f strings? Or format method or function? Posts: 6,920 Threads: 22 Joined: Feb 2020 Jan-13-2024, 09:05 PM (This post was last modified: Jan-13-2024, 09:06 PM by deanhystad.) The print statement defaults to a single space as a delimiter, not a tab. You can set the seperator using the sep="\t" in the print command Look at str.join(). It does what I think you are asking. Posts: 21 Threads: 5 Joined: Jan 2024 Jan-13-2024, 09:11 PM (This post was last modified: Jan-13-2024, 09:11 PM by johnywhy.) (Jan-13-2024, 09:05 PM)deanhystad Wrote: Look at str.join(). It does what I think you are asking. Can this be simplified with f or format or any other way? delimited = "\t".join((str(idx), choice, '\n')) Posts: 26 Threads: 1 Joined: Jan 2024 (Jan-13-2024, 09:11 PM)johnywhy Wrote: (Jan-13-2024, 09:05 PM)deanhystad Wrote: Look at str.join(). It does what I think you are asking. Can this be simplified with f or format or any other way? delimited = "\t".join((str(idx), choice, '\n')) what is your actual use case? Do you need to print out elements of lists/tuples of arbitrary length or do you always have only 2 elements? Depending on what you are writing, you can just use format string like print(f"{idx[0]}\t{idx[1]}\n") If you need something more complex, you can define your own __str__ method in a data class and just print that. Posts: 21 Threads: 5 Joined: Jan 2024 Jan-13-2024, 09:40 PM (This post was last modified: Jan-13-2024, 09:40 PM by johnywhy.) (Jan-13-2024, 09:30 PM)sgrey Wrote: what is your actual use case? Do you need to print out elements of lists/tuples of arbitrary length or do you always have only 2 elements? Always 2 elements followed by newline. Quote:Depending on what you are writing, you can just use format string like print(f"{idx[0]}\t{idx[1]}\n") I'm just trying to assemble the string here, not print it. How to do that with f? My terms are just str(idx) and choice, with a trailing newline. Quote:you can define your own __str__ method in a data class. I'd like to learn how, but sounds even more complicated. Posts: 26 Threads: 1 Joined: Jan 2024 Jan-13-2024, 09:48 PM (This post was last modified: Jan-13-2024, 09:49 PM by sgrey.) (Jan-13-2024, 09:40 PM)johnywhy Wrote: I'm just trying to assemble the string here, not print it. How to do that with f? My terms are just str(idx) and choice, with a trailing newline. You are just writing out elements of a tuple manually and you can directly use that string without a print statement. You can either just do something like t = ('a', 'b') myString = f"{t[0]}\t{t[1]}\n"which does look busy, but in reality is a simple statement. Or if you need to do it in many places in your code, define a function that will return this formatted string from your tuple and just call it. (Jan-13-2024, 09:40 PM)johnywhy Wrote: I'd like to learn how, but sounds even more complicated. If you need custom data behavior, you can define a class for your data. In python if you place __str__ method in your class, you can then directly put an instance of that class in a print statement and it will invoke this method to print what you define in it. Posts: 21 Threads: 5 Joined: Jan 2024 Jan-13-2024, 10:11 PM (This post was last modified: Jan-13-2024, 10:40 PM by johnywhy.) Thanks. I can see how putting the items into a tuple first would help in some cases, but i think it doesn't give much benefit here. Your f statement doesn't use the entire tuple t, it uses each element individually. So you're putting things into a tuple, and then taking them out again. Your f statement is less to type than my join, which is good. # join myString = "\t".join((str(idx), choice, '\n')) # f myString = f"{str(idx)}\t{choice}\n"Would the f statement execute faster on a large set? (i don't mean more elements, i mean more rows with these same 2 elements per row) One thing i don't like is that i can't embed spaces in the f statement for visual clarity, because the spaces would become part of the string. myString = f" {str(idx)} \t {choice} \n" Posts: 26 Threads: 1 Joined: Jan 2024 you don't need to call str on a variable inside string format, so you would write it as myString = f"{idx}\t{choice}\n" And you are correct, string format of that kind is not the most readable. There is not much you can do about it as far as I know. The best solution probably would be a custom object that will deal with formatting internally and provide you a good interface. And as far as performance go, it will vary which one is faster, join or string formatting. In this case formatting probably will be a bit faster. |