Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

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
Comment

PREVIOUS NEXT
Code Example
Python :: get_absolute_url django 
Python :: uploading folder in google colab 
Python :: enumerate string pythonm 
Python :: cv2 blue color range 
Python :: check if numpy array contains only duplicates 
Python :: how to print correlation to a feature in pyhton 
Python :: how to get key value in nested dictionary python 
Python :: python thread with return values? 
Python :: sort folders content by name python 
Python :: factorial of a number in python 
Python :: index from multiindex pandas 
Python :: python program to check if binary representation is a palindrome 
Python :: how to take multiple line input in python 
Python :: pandas dataframe.to_dict 
Python :: add image pptx python 
Python :: python pop 
Python :: django url patterns static 
Python :: generate random integers in a range python 
Python :: how to set and run flask app on terminal 
Python :: ffill python 
Python :: area of trapezium 
Python :: python if and 
Python :: check if two columns are equal pandas 
Python :: pandas cartesian product 
Python :: python beginner projects 
Python :: find pdf encrypted password with python 
Python :: how to create background images in tkinter 
Python :: copy content from one file to another in python 
Python :: numpy get diagonal matrix from matrix 
Python :: python script to scrape data from website 
ADD CONTENT
Topic
Content
Source link
Name
8+7 =