banner

© 2004-21 ict4u.net

Find Position of Character

This determines if string findstr occurs in string, or in a substring of string if a start position and ending position are given.
It also demonstrates how to count the occurrences of a string

# A Knifton
# Demo of finding character in string
#

print("Finding Characters in a string\n\n")
myText = "On June 27th, YouTuber Fapplet uploaded a montage parody video"
#
# Find character in a string
charPos = myText.find("e")
print("Character e found at position:{} in string:\n{}\n\n".format(charPos, myText))
#
# find subsequent character
charPos = myText.find("e",charPos+1)
print("Next Character e found at position:{} in string:\n{}\n\n".format(charPos, myText))
#
# Count the number of times a character occurs in a string
charCount = myText.count("e")
print("Character e occurs {} times in the string:\n{} ".format(charCount, myText))