Q:

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

-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)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now