Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python inserting into middle of a string

I am new to python, have a book and playing around so please be kind. Mainly I have been playing with ASCII art

What I am trying to do is hide some art into some text. So say I have a string print out "word", what I want to do is write a function that helps me insert a character into the middle of that word regardless of how long the word is, it always places my art character into the middle of the word. So my output would be 'wo-rd'. What I have started with so far is:

def dashinsert(str):
    return str[:2] + '-' + str[2:]

I know that this is no where close on where it needs to be, and I am only a beginner, but any direction to look is appreciated, I am not sure I am doing some of this right either

My goal is to learn from this and then insert random characters into words at various positions to make art in text. ie I type a paragraph and the art will insert itself as a function. Right now I am just trying to get this insert portion down

like image 278
Tye Chamberlain Avatar asked Mar 09 '23 02:03

Tye Chamberlain


1 Answers

Use double slashes for integer divide

def dashinsert(str):
    midPoint = len(str)//2
    return str[:midPoint] + '-' + str[midPoint:]
like image 139
Viliami Avatar answered Mar 12 '23 00:03

Viliami