Python Forum
a function to layout a string
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
a function to layout a string
#1
before i write this function, i'm curious if such a thing already exists in Python. the function would be given a series of arguments or a list with a variable sequence of values. for each value that is an int, it specifies to change a current position indicator. for each value that is a string, it specifies a sequence to place into the result string. the result would be built virtually and return a string with the laid out result.

layout(8,'foo',-1,'bar') -> 'bar foo'
layout('foo',2,'bar','xyz') -> 'fobarxyz'
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
I don't believe there is a function that does specifically what you're asking. You may need to implement that yourself. I am now curious why you would want to generate strings in this manner. Is there any reason the string method format cannot be used for your purposes? If you are unaware, format works like this:
fString = "The first parameter goes here: {}\nThe second parameter goes here: {}\nand all subsequent parameters go in place of any subsequent pairs of curly braces.".format("FIRST", "SECOND")
print(fString)
The output would look like this:
Output:
The first parameter goes here: FIRST The second parameter goes here: SECOND and all subsequent parameters go in place of any subsequent pairs of curly braces.
In case you are new to python and you really need to implement the function you describe for some reason, string slicing will help you a great deal. Here's a quick tutorial:

The syntax for slicing a string, or any iterable like a list is:
aString = "This is our hypothetical string."
print("'{}'".format(aString[5:7]))
Output:
'is'
The index after the colon is non-inclusive. The slice starts at the first index and ends at the position before the second.

Omitting the first index starts at the beginning of the string:

print("'{}'".format(aString[:11])
Output:
'This is our'
Omitting the second index starts at the first index and continues to the end of the string:

print("'{}'".format(aString[12:]))
Output:
'hypothetical string.'
This should simplify the function you are attempting to write.
Reply
#3
i need position control of exactly the place certain text goes. i will implement.

i am not new to Python unless you can figure out how to make time go backwards. a quick inaccurate check: this is about my 1400th script, counting lots of one-function files and probably a few duplicates. most of the command scripts do stuff on AWS.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
I made a mistake in the string slicing tutorial I wrote here. The last slice would actually be:
Output:
'hypothetical string'
Also, I'm an American who speaks 2 languages: English and Japanese. What does that make me?

Jokes aside, I hope my reply was helpful.

Apparently, I do struggle with English though. Having 1400 scripts to your name, you would understand something as basic as slicing.
Reply
#5
i probably do slicing every day.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#6
(May-12-2019, 08:59 PM)keames Wrote: Also, I'm an American who speaks 2 languages: English and Japanese. What does that make me?

someone was reading my posts over on Ubuntu forum.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Convert a string to a function mikepy 8 2,424 May-13-2022, 07:28 PM
Last Post: mikepy
  Make a Function from Name String jge047 4 3,271 Dec-18-2017, 05:05 AM
Last Post: Terafy

Forum Jump:

User Panel Messages

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