In: Computer Science
} 1. write a function that takes a string as parameter, return true if it’s a valid variable name, false otherwise. You can use keyword module’s iskeyword() to determine is a string is keyword.
import keyword
keyword.iskeyword(var) #returns true or false
}2. write a function that returns the length of a string (without using len() function)
}3. write a function that counts number of vowels in a string
}4. write a function that checks if a word is palindrome
PYTHON PROGRAMMING
Implemented the code as per the requirement. As python is
indentation specific, you may not get the formatted text while
copying the code,
so I'm attaching the screenshots of the code for reference. Please
make sure when you are executing the below code you have same
format, especially tabs.
Please comment if any modification required or if you need any help.
1).
Code:
=====
import keyword
def check_valid_var_name(word):
if(keyword.iskeyword(word)):
return False
return True
print(check_valid_var_name("variable"))
print(check_valid_var_name("break"))
print(check_valid_var_name("while"))
code and output screenshot:
=====================
2).
code:
====
def calc_length(str):
count=0
for i in str:
count = count+1
return count
print(calc_length("this is a "))
print(calc_length("string"))
code and output screenshot:
======================
3).
code:
====
def count_vowels(str):
count=0
vowels = ['A','a','E','e','I','i','O','o','U','u']
for i in str:
if(i in vowels):
count = count+1
return count
print(count_vowels("this is a "))
print(count_vowels("education"))
code and output screenshot:
======================
4).
code:
=====
def is_palindrome(str):
for i in range(len(str)):
if(str[i]!=str[len(str)-1-i]):
return False
return True
print(is_palindrome("madam"))
print(is_palindrome("education"))
code and output screenshot:
=====================