how to replace the last character of a string in python

sample_string = "712345677890 173445667"
# if i wanted to do:
new_string=sample_string.replace("7", r)
print new_string
#you won't get your results
#Output:
r123456rr890 1r344566r
#but i wanted it to be
712345677890 17344566r
#there are different methods of solving this problem but i use this one
new_string=sample_string [:-1] + "r" # you can make change the amount of last letters getting replaced like for example the last three letters you would change [:-1] to [:-3]
#Output:
712345677890 17344566r #yay this is it

Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
replace last 2 characters in string python python replace last match character in string python change the last character of a string python replace last 3 characters in string replace the last character with python python replace last n characters with string python replace last N characters python replace last characters in string python replace last two character in string change last char of a string py change last 4 chars of a string py reduce the last character of a string in python python3 replace last letter in string if it matches python replace last character replace last character in string python using regex replace last 4 characters string python replace last character of a list python replace last character in a list python replace last character in list python replace last character in string python index replace last occurrence of a string in python replace last one specific character in python python re replace last part of string till a symbol python replace last string character python replace last string replace last element in string python how to change the last word in a string in python how to replace only last occurance of a chranecter in pytho python how to change last characters of a string replace last word in string python replace last character of a string python python replace character at last index in string replace last caracter in string python by using end of string caracter change last character of string python python replace last string just once python replace last occurrence of character in string Find and replace last occurrence of a character in string - Python how to replace last character of string in python replace last 3 characters in string python string replace last python replace last occurance in string python replace last instance of string python python change last char of string python3 change last char of string python string find and replace last python string find and replace laste python string replace from last how to replace a last element of a string python replace last occurrence of character in string python replace character in string python last python replace last match python replace last occurrence of char replace last character in string python python search and replace last occurrence in string string replace last char python How to edit only the last character in a string in python how to replace the the last character in a string in python python string replace last 3 characters python replace last 4 characters of string python replace last found char python change last character in string python replace not working for last character python replace last character in string replace last python how to replace second last of string in python python string replace last occurrence remove last 4 letters from string python how to remove last before, in string ptyhon how to skip last symbol in string python remove last two letters from string python how to check for the length of an element in a list python replace last two characters string python replace last characters in string python python replace last occurrence in string truncate last char python how to replace the last character of a string in python
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source