Inserting characters in strings in python

后端 未结 4 2065
有刺的猬
有刺的猬 2021-02-15 11:22

I\'m doing homework and I have all except this last problem figured out. I can\'t seem to figure out how to get a character not to show up if it\'s inserted at a larger insertio

4条回答
  •  孤街浪徒
    2021-02-15 11:57

    Just keep it simple. Check to see if the position is greater than the length of the word then just print the word, else proceed with your logic:

    C = input("Choose your charecter to insert. ")
    P = int(input("Choose your character's position. "))
    S = input("Choose your string. ")
    
    if P > len(S):
        print(S)
    else:
        st = S[:P] + C + S[P:]
    
        print(st)
        print(C, P, S)
    

提交回复
热议问题