Answers for "python change last character in string"

64

python remove last character from string

str =  "string"
str = str[:-1]  # Returns "strin"
Posted by: Guest on July-25-2020
1

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
Posted by: Guest on January-23-2021
4

last character of string in python

a = '123456789'

#to get last char, acess index -1
print(a[-1])

#this works the same as:
print(a[len(a)-1])

#instead of 1, subtract n to get string's nth char from last
#let n = 4
print(a[-4])

#result:
9
9
6
Posted by: Guest on September-08-2020

Code answers related to "python change last character in string"

Python Answers by Framework

Browse Popular Code Answers by Language