Python Forum

Full Version: Print strings enclosed with a character with different characters
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello everyone, I'm currently learning for loops right now and I did a basic program that takes string inputs and an integer input. The program will print the string and will split the strings based on the integer input. How do I make it something like this?

S = jhdsjalhdshja21dasj
N = 5
-------
|jhdsj|
|alhds|
|hja21|
|dasj |
-------

My code:

string = input("String = ")
length = input("Length = ")

for i in range(0, len(string), length):
    print("|",s[i:i+n,"|")
You made some mistakes. Use an IDE not to make them
string = "jhdsjalhdshja21dasj"
length = 5

for i in range(0, len(string), length):
    print("|", string[i:i + length], "|")