Reputation: 175
In my python program the user will enter a string.
If the user entered the string less then five letters then it tells the user
the string is too short!
If the string entered is more then 5 letters then it will make the uppercase the last five letters of the string.
For example:
input: "heroiszero"
output: SZERO
expected output: heroiSZERO
myString= input("Enter the string you want:- ")
print("The String you Entered is :- ", myString)
if len(myString) <= 5:
print("String is too short!")
else:
print(myString[-5:].upper())
What changes should I have change to get my exact output I want?
Upvotes: 0
Views: 171
Reputation: 7313
You can remove the last 5 chars from the string , save it in a variable, upper
it and put it back at the end of the string.
myString= input("Enter the string you want:- ")
print("The String you Entered is :- ", myString)
if len(myString) <= 5:
print("String is too short!")
else:
temp = myString[-5:] #getting last 5 chars of string
myString = myString[:-5] #removing last 5 chars from the string
temp = temp.upper() #making the saved 5 chars capital
myString = myString+temp #adding the capital 5 chars back to string
print(myString)
If you want it to be simpler with the same concept:
else:
temp = myString[-5:].upper()
myString = myString[:-5] + temp
Testing the code:
Enter the string you want:- heroiszero
The String you Entered is :- heroiszero
heroiSZERO
Upvotes: 1
Reputation: 1020
By doing print(myString[-5:].upper())
you are only printing out the last 5 characters of that string.
To get your desired output you must concatenate that with the remaining, initial part of your string like this
print(myString[:-5] + myString[-5:].upper())
However, is this really what you need? I thought maybe if the string is longer than 5 characters one might want to leave the first characters intact and capitalize all the remaining ones like this
print(myString[:5] + myString[5:].upper())
Upvotes: 2
Reputation: 1457
try this :
myString= input("Enter the string you want:- ")
print("The String you Entered is :- ", myString)
if len(myString) <= 5:
print("String is too short!")
else:
print(myString[:-5]+myString[-5:].upper())
myString[-5:]
select the last five items, ignore the others, and then upper()
convert it to uppercase.
You need to print the first part of the string too if you want to display it.
myString[:-5]
select all the characters of the string but not the last 5.
myString[:-5] + myString[-5:]
is the same as myString
.
Upvotes: 2
Reputation: 27879
Last line should be:
print(myString[:-5] + myString[-5:].upper())
So you take the string as is up to last five characters and use +
, which is concatenation, with last five characters converted to upper()
.
Upvotes: 4
Reputation: 11927
You are only taking last 5 index and making it upper and printing.
You must include the other characters ie from index 0 to 5th last index.
It can be done with slicing like this..
myString= input("Enter the string you want:- ")
print("The String you Entered is :- ", myString)
if len(myString) <= 5:
print("String is too short!")
else:
print(myString[:-5] + myString[-5:].upper())
myString[:-5]
gives the substring from index 0 to 5th last index
myString[-5:].upper()
takes last 5 characters and make it upper case
myString[:-5] + myString[-5:].upper()
concatinates both and gives the result as you need
Upvotes: 2