Created
January 5, 2017 15:30
-
-
Save nicholaskajoh/35f6d02a9e041a2fb4639d371a3fcd4a to your computer and use it in GitHub Desktop.
Fanan Kwanga's Assignment Q2. Choose any method your like. I think M1 looks more mature and is faster.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
# method 1 | |
# using Regular Expressions AKA RegEx-es | |
def lenFunction1(): | |
print("#### Method 1 ####") | |
sentence = str(input("Enter valid sentence:")) | |
# use RegEx substitution | |
# param 1: find | |
# param 2: replace | |
# param 3: string | |
sentence = re.sub(' ', '', sentence) | |
sentence_len = len(sentence) | |
print("The number of characters contained in your sentence is: ", sentence_len) | |
# method 2 | |
# Using a loop; 'for' in this case | |
def lenFunction2(): | |
print("#### Method 2 ####") | |
sentence = str(input("Enter valid sentence:")) | |
sentence_len = 0 | |
for letter in sentence: | |
if letter != ' ': | |
sentence_len+=1 | |
print("The number of characters contained in your sentence is: ", sentence_len) | |
if __name__ == '__main__': | |
lenFunction1() # 1 | |
lenFunction2() # 2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment