Q:

Write a program to remove characters from a string starting from zero up to n and return a new string.

belongs to collection: Python Basic Exercises for Beginners

-1

Remove first n characters from a string

Write a program to remove characters from a string starting from zero up to n and return a new string.

For example:

  • remove_chars("pynative", 4) so output must be tive. Here we need to remove first four characters from a string.
  • remove_chars("pynative", 2) so output must be native. Here we need to remove first two characters from a string.

Noten must be less than the length of the string.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Hint:

Use string slicing to get the substring. For example, to remove the first four characters and the remeaning use s[4:].

Solution:

def remove_chars(word, n):
    print('Original string:', word)
    x = word[n:]
    return x

print("Removing characters from a string")
print(remove_chars("pynative", 4))
print(remove_chars("pynative", 2))

 

exaplanation in video:

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Write a function to return True if the first and l... >>
<< Write a program to accept a string from the user a...