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

برمجة بايثون | Python programming


مهني|Professional
  • 2025-03-23

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

Note: n must be less than the length of the string

 

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

هل كان الشرح مفيد؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...