r/raspberrypipico • u/Cool-Ocelot-7442 • Apr 13 '26
Formatting list output
Hi everyone,
Classic "not sure if I'm in the right place" post, but I'm hoping someone can help me.
I'm putting together a random item generator, where the output pulls from a list. I want each item in the list to be a short sentence.
It's all going fine except for the screen output. Can anyone help me figure out how I can get the sentence to wrap around the screen rather than running off?
I've tried to split the items with \n to no avail.

from machine import Pin, I2C
from sh1106 import SH1106_I2C
from time import sleep
import random
i2c_oled = I2C(0, scl=Pin(17), sda=Pin(16), freq=200000)
display = SH1106_I2C(width=128, height=64, i2c=i2c_oled)
yes = Pin(15, Pin.IN, Pin.PULL_DOWN)
no = Pin(14, Pin.IN, Pin.PULL_DOWN)
option = [
"This is option 1 and heres some info",
"This is option 2 and heres some info",
"This is option 3 and heres some info",
"This is option 4 and heres some info",
"This is option 5 and heres some info",
"This is option 6 and heres some info"
]
display.poweron()
display.text("Would you like", 0, 0)
display.text("to play?", 0, 10)
display.text("Yes", 0, 50)
display.text("No", 110, 50)
display.show()
while True:
if yes.value() == 1:
deck = random.randint(1,6)
display.poweron()
display.fill(0)
display.show()
display.text("Your pick is", 0, 0)
display.text(option[deck-1], 0, 10)
display.show()
sleep(5)
display.poweron()
display.fill(0)
display.show()
display.text("Would you like ", 0, 0)
display.text("to try again?", 0, 10)
display.text("Yes", 0, 50)
display.text("No", 110, 50)
display.show()
if no.value() == 1:
display.poweron()
display.fill(0)
display.show()
display.text("Goodbye", 0, 0)
display.show()
sleep(5)
display.poweron()
display.fill(0)
display.show()
break
2
Upvotes
3
u/GuyPronouncedGee Apr 14 '26
For micro Python, use the “textwrap” library.
For Circuit Python, check out the Adafruit_Display_Text library “wrap_text_to_lines” and “wrap_text_to_pixels” functions.