Skip to content

Instantly share code, notes, and snippets.

@petrusnog
Created February 10, 2022 04:22
Show Gist options
  • Save petrusnog/ad30bb1547f8edbf71c270843181b868 to your computer and use it in GitHub Desktop.
Save petrusnog/ad30bb1547f8edbf71c270843181b868 to your computer and use it in GitHub Desktop.
Script to study about string indexes and substrings.
def set_line(word_length):
line = '+'
line += word_length * '---+'
line += '\n'
return line
def set_letters(word):
count = 1
letters = ''
for letter in word:
letters += '| {} '.format(letter)
if (count == len(word)):
letters += '|\n'
count += 1
return letters
def set_indexes(word, positive = True):
indexes = ''
count = 0 if positive else -len(word)
limit = len(word)-1 if positive else -1
for letter in word:
if positive:
index = '*' if count > 9 else count
else:
index = '* ' if count < -9 else count
space = ' ' if positive else ' '
indexes += '{}{}'.format(index, space)
if (count == limit):
final_index = count+1 if positive else ''
indexes += '{}\n'.format(final_index)
count += 1
return indexes
print('\nS U B S T R I N G S E M P Y T H O N :)\n\n')
word = str(input('Digite uma string: '))
substring_graphic = '\n' + set_line(len(word))
substring_graphic += set_letters(word)
substring_graphic += set_line(len(word))
substring_graphic += set_indexes(word)
substring_graphic += set_indexes(word, False)
print(substring_graphic)
incluido = int(input('Incluído: '))
excluido = int(input('Excluído: '))
print(word[incluido:excluido])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment