In Python, string object is considered as a sequence of characters represented by double quotes (“”) or single quotes (”).
To see how to reverse the string in PowerShell, have a look at https://tekcookie.com/reverse-a-string-in-powershell/
Accessing characters through array index
s = "tekcookie.com"
#To get the first character
print("First Character: ", s[0])
#To get the last character
print("Last Character: ", s[-1])
Result:
Display full string
s = "tekcookie.com"
#To display full string
print("String is: ", s)
#Iterating through all indexes
print("String is: ", s[0:len(s):1])
#In short form
print("String is: ", s[::1])
#Skipping alternate characters
print("Skipping alternate characters in String: ", s[::2])
Result:
Reversing the String
Reverse string by iterating through array
s = "tekcookie.com"
#Iterating all indexes -
print("Reversed string:", s[-1:-len(s)-1:-1])
#In Short
print("Reversed string:", s[::-1])
Result
Reverse string using for loop
s = "tekcookie.com"
strn = ""
for i in s:
strn = i + strn
print("Reversed String:", strn)
Result:
Reverse string using reversed method
s = "tekcookie.com"
print("Reversed string:", ''.join(reversed(s)))
Result:
If you know of other ways to reverse the string in Python, comment below with the code !!!
Thank you for reading the article.
Recent Comments