Vertical Print String - Python3.2

2024/11/13 8:03:07

I'm writing a script that will take as user inputed string, and print it vertically, like so:

input = "John walked to the store"output = J w t t so a o h th l   e on k     re     ed

I've written most of the code, which is as follows:

import sysdef verticalPrint(astring):wordList = astring.split(" ")wordAmount = len(wordList)maxLen = 0for i in range (wordAmount):length = len(wordList[i])if length >= maxLen:maxLen = length### makes all words the same length to avoid range errors ###for i in range (wordAmount):if len(wordList[i]) < maxLen:wordList[i] = wordList[i] + (" ")*(maxLen-len(wordList[i]))for i in range (wordAmount):for j in range (maxLen):print(wordList[i][j])def main():astring = input("Enter a string:" + '\n')verticalPrint(astring)main()

I'm having trouble figure out how to get the output correct. I know its a problem with the for loop. It's output is:

input = "John walked"output = Johnwalked

Any advice? (Also, I want to have the print command used only once.)

Answer

Use itertools.zip_longest:

>>> from itertools import zip_longest
>>> text = "John walked to the store"
for x in zip_longest(*text.split(), fillvalue=' '):print (' '.join(x))
...     
J w t t s
o a o h t
h l   e o
n k     re     ed      
https://en.xdnf.cn/q/72197.html

Related Q&A

How to remove small particle background noise from an image?

Im trying to remove gradient background noise from the images I have. Ive tried many ways with cv2 without success.Converting the image to grayscale at first to make it lose some gradients that may hel…

Running commands from within python that need root access

I have been playing around with subprocess lately. As I do more and more; I find myself needing root access. I was wondering if there is an easy way to enter the root password for a command that needs …

How not to plot missing periods

Im trying to plot a time series data, where for certain periods there is no data. Data is loaded into dataframe and Im plotting it using df.plot(). The problem is that the missing periods get connected…

Disabling std. and file I/O in Python sandbox implementation

Im trying to set up a Python sandbox and want to forbid access to standard and file I/O. I am running the sandbox inside of a running Python server.Ive already looked at modules like RestrictedPython a…

Extract edge and communities from list of nodes

I have dataset which has more than 50k nodes and I am trying to extract possible edges and communities from them. I did try using some graph tools like gephi, cytoscape, socnet, nodexl and so on to v…

Why is this usage of python F-string interpolation wrapping with quotes?

Code in question:a = test# 1) print(f{a}) # test# 2) print(f{ {a} }) # {test}# 3) print(f{{ {a} }}) # {test}My question is, why does case two print those quotes?I didnt find anything explicitly in the…

Adding a matplotlib colorbar from a PatchCollection

Im converting a Shapely MultiPolygon to a PatchCollection, and first colouring each Polygon like so:# ldn_mp is a MultiPolygon cm = plt.get_cmap(RdBu) num_colours = len(ldn_mp)fig = plt.figure() ax = f…

Mac 10.6 Universal Binary scipy: cephes/specfun _aswfa_ symbol not found

I cant get scipy to function in 32 bit mode when compiled as a i386/x86_64 universal binary, and executed on my 64 bit 10.6.2 MacPro1,1.My python setupWith the help of this answer, I built a 32/64 bit …

python: numpy list to array and vstack

from scipy.io.wavfile import read filepath = glob.glob(*.wav) rates = [] datas = [] for fp in filepath:rate, data = read(fp)rates.append(rate)datas.append(data)I get a list datas which is :[array([0, 0…

Django Unittests Client Login: fails in test suite, but not in Shell

Im running a basic test of my home view. While logging the client in from the shell works, the same line of code fails to log the client in when using the test suite.What is the correct way to log the …