search
[last updated: 2023-09-05]
Python home page
Python: Programming
-----
str1[-5:]
returns: ghijk - ie. the last 5 characters of the string
str1[1:-4]
returns: bcdefg - ie. all characters except first char and last 4 chars
str1[::2] - returns every other character
# printing original string
print("The original string is : " + test_str)
# initializing N
N = 3
# Get Nth word in String
# using loop
count = 0
res = ""
for ele in test_str:
if ele == ' ':
count = count + 1
if count == N:
break
res = ""
else :
res = res + ele
# printing result
print("The Nth word in String : " + res)
Output :
The original string is : GFG is for Geeks
The Nth word in String : for
.
.
.
eof