Python Forum
a function to layout a string - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: a function to layout a string (/thread-18195.html)



a function to layout a string - Skaperen - May-08-2019

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'


RE: a function to layout a string - keames - May-11-2019

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.


RE: a function to layout a string - Skaperen - May-12-2019

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.


RE: a function to layout a string - keames - May-12-2019

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.


RE: a function to layout a string - Skaperen - May-13-2019

i probably do slicing every day.


RE: a function to layout a string - Skaperen - May-13-2019

(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.