Python Forum
Print strings enclosed with a character with different characters
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Print strings enclosed with a character with different characters
#1
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,"|")
Reply
#2
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], "|")
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Replacing a few characters of a specified character but not all of them from a string fatherted99 7 3,180 Aug-13-2020, 09:08 AM
Last Post: fatherted99
  Change each character of list into an different characters Angry_bird89 1 2,044 Jun-19-2020, 08:29 AM
Last Post: Angry_bird89
  Unexpected character after line continuation character joshyb123 5 10,602 Sep-05-2018, 08:08 AM
Last Post: perfringo
  unexpected character after line continuation character error newbie 10 14,583 Aug-09-2018, 06:07 PM
Last Post: nilamo
  Strings inside other strings - substrings OmarSinno 2 3,645 Oct-06-2017, 09:58 AM
Last Post: gruntfutuk

Forum Jump:

User Panel Messages

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