10/11


#1

pyg = ‘ay’

original = raw_input(‘Enter a word:’)

if len(original) > 0 and original.isalpha():
word=original.lower()
first=word[0]
new_word=word+first+pyg
new_word=new_word[1:len(new_word)]
print new_word
else:
print ‘empty’
’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’’'
in setting new_word to new_word[1:len(new_word)] shouldn’t it be new_word[1:len(new_word)-1]
BC new_word[] counts from 0 but len() counts from 1
i mean if i have word like s=“hello” if i want to get last letter “o”. i will use s[4] to get "o"
if i get it’s length by len(s) i will get 5
any help??


#2

this:

new_word[1:len(new_word)]

is called string slicing, you take a slice of string which is stored in new_word variable

list slicing has the following general syntax:

"your string"[start_value:stop_value]

but the slicing is exclusive (lesser then <) then the stop value, not lesser then or equal to (<=) the stop value, as such, we don’t have to subtract one


#3

Additionally you can omit the boundaries to indicate that you want to/from the end, so for example [:] (both boundaries omitted) will copy the whole thing because that says from the start to the end


#4

In the current case, you can omit the end boundary as follows, which copies all characters except for the one at index 0

new_word = new_word[1:]

#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.