1

please someone help me! I am trying to make a simple program where the user can insert a string into an already existing string(sorry if that's confusing!)

this is my code:

firststring = input("enter a string: ")
secondstring = input("enter a second string: ")
position = input("where would you like to place the second string?")
if possition.isdigit == True:
    print() <--#help me here

so can someone please help me?

1
  • 1
    Well you can't insert a string inside another string as strings are immutable. You need to take slices of strings based on position and concatenate them. Commented Aug 7, 2021 at 2:56

2 Answers 2

4

You can use string slicing to print the first part of the first string, then the second string, then the remainder of the first string.

position = int(input("where would you like to place the second string?"))
print(firststring[:position] + secondstring + firststring[position:]
Sign up to request clarification or add additional context in comments.

Comments

1

You can go with list slicing. firststring[:int(position)] will get all characters till specific index, then concantenate the second string and firststring[int(position):] will get all characters from the index position to the end

position = input("where would you like to place the second string?")
if position.isdigit() :
    string3=firststring[:int(position)]+secondstring+firststring[int(position):]
    print(string3)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.