Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Tab Delimited Strings?
#1
the following tab-delimits each item
print ("a", "b")
a	b
Is 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?
Reply
#2
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.
johnywhy likes this post
Reply
#3
(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'))
Reply
#4
(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.
johnywhy likes this post
Reply
#5
(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.
Reply
#6
(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.
johnywhy likes this post
Reply
#7
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"
Reply
#8
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.
johnywhy likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to understand strings and lists of strings Konstantin23 2 772 Aug-06-2023, 11:42 AM
Last Post: deanhystad
  Splitting strings in list of strings jesse68 3 1,779 Mar-02-2022, 05:15 PM
Last Post: DeaD_EyE
  dataframe write to tab delimited differs from Excel koh 0 2,009 Aug-01-2021, 02:46 AM
Last Post: koh
  Appending data into a file in tab delimited format metro17 1 4,152 Aug-06-2019, 07:34 AM
Last Post: fishhook
  Finding multiple strings between the two same strings Slither 1 2,529 Jun-05-2019, 09:02 PM
Last Post: Yoriz
  Delimited Values to ROW - Lucky Train ? karthi_python 1 2,352 May-30-2019, 06:40 AM
Last Post: karthi_python
  Creating a delimited file from DB Table anubhav2020 9 7,134 Sep-19-2018, 05:22 PM
Last Post: Axel_Erfurt
  Load Comma Delimited csv to Nested Dictionary Huck 2 7,994 Apr-30-2018, 04:21 PM
Last Post: Huck
  lists, strings, and byte strings Skaperen 2 4,235 Mar-02-2018, 02:12 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020