How to detect lowercase letters in Python? - python

I need to know if there is a function that detects the lowercase letters in a string. Say I started writing this program:
s = input('Type a word')
Would there be a function that lets me detect a lowercase letter within the string s? Possibly ending up with assigning those letters to a different variable, or just printing the lowercase letters or number of lowercase letters.
While those would be what I would like to do with it I'm most interested in how to detect the presence of lowercase letters. The simplest methods would be welcome.

To check if a character is lower case, use the islower method of str. This simple imperative program prints all the lowercase letters in your string:
for c in s:
if c.islower():
print c
Note that in Python 3 you should use print(c) instead of print c.
Possibly ending up with assigning those letters to a different variable.
To do this I would suggest using a list comprehension, though you may not have covered this yet in your course:
>>> s = 'abCd'
>>> lowercase_letters = [c for c in s if c.islower()]
>>> print lowercase_letters
['a', 'b', 'd']
Or to get a string you can use ''.join with a generator:
>>> lowercase_letters = ''.join(c for c in s if c.islower())
>>> print lowercase_letters
'abd'

There are 2 different ways you can look for lowercase characters:
Use str.islower() to find lowercase characters. Combined with a list comprehension, you can gather all lowercase letters:
lowercase = [c for c in s if c.islower()]
You could use a regular expression:
import re
lc = re.compile('[a-z]+')
lowercase = lc.findall(s)
The first method returns a list of individual characters, the second returns a list of character groups:
>>> import re
>>> lc = re.compile('[a-z]+')
>>> lc.findall('AbcDeif')
['bc', 'eif']

There are many methods to this, here are some of them:
Using the predefined str method islower():
>>> c = 'a'
>>> c.islower()
True
Using the ord() function to check whether the ASCII code of the letter is in the range of the ASCII codes of the lowercase characters:
>>> c = 'a'
>>> ord(c) in range(97, 123)
True
Checking if the letter is equal to it's lowercase form:
>>> c = 'a'
>>> c.lower() == c
True
Checking if the letter is in the list ascii_lowercase of the string module:
>>> from string import ascii_lowercase
>>> c = 'a'
>>> c in ascii_lowercase
True
But that may not be all, you can find your own ways if you don't like these ones: D.
Finally, let's start detecting:
d = str(input('enter a string : '))
lowers = [c for c in d if c.islower()]
# here i used islower() because it's the shortest and most-reliable
# one (being a predefined function), using this list comprehension
# is (probably) the most efficient way of doing this

You should use raw_input to take a string input. then use islower method of str object.
s = raw_input('Type a word')
l = []
for c in s.strip():
if c.islower():
print c
l.append(c)
print 'Total number of lowercase letters: %d'%(len(l) + 1)
Just do -
dir(s)
and you will find islower and other attributes of str

import re
s = raw_input('Type a word: ')
slower=''.join(re.findall(r'[a-z]',s))
supper=''.join(re.findall(r'[A-Z]',s))
print slower, supper
Prints:
Type a word: A Title of a Book
itleofaook ATB
Or you can use a list comprehension / generator expression:
slower=''.join(c for c in s if c.islower())
supper=''.join(c for c in s if c.isupper())
print slower, supper
Prints:
Type a word: A Title of a Book
itleofaook ATB

If you don't want to use the libraries and want simple answer then the code is given below:
def swap_alpha(test_string):
new_string = ""
for i in test_string:
if i.upper() in test_string:
new_string += i.lower()
elif i.lower():
new_string += i.upper()
else:
return "invalid "
return new_string
user_string = input("enter the string:")
updated = swap_alpha(user_string)
print(updated)

Related

Write a function that takes a string parameter and returns a new string with all the letters of the alphabet that are not in the argument string

Write a function called missing_letters that takes a string parameter and returns a new string with all the letters of the alphabet that are not in the argument string. The letters in the returned string should be in alphabetical order.
It should also use the global variable alphabet. It should use this global variable directly, not through an argument or a local copy. It should loop over the letters in alphabet to determine which are missing from the input parameter.
The function missing_letters should combine the list of missing letters into a string and return that string.
Write a loop over the strings in list test_miss and call missing_letters with each string. Print a line for each string listing the missing letters. For example, for the string "aaa", the output should be the following.
aaa is missing letters bcdefghijklmnopqrstuvwxyz
I tried my best to solve this. The program is working but not giving
the desired output.
test_miss = ["b","zzz"]
def missing_letters(s):
missingAlphabets = ""
global alphabet
for c in s:
i=0
while i < len(alphabet):
if alphabet[i] not in c:
missingAlphabets += alphabet[i]
i += 1
sortedmissingAlphabetslists = sorted(missingAlphabets)
sortedmissingAlphabets = ''.join(sortedmissingAlphabetslists)
return sortedmissingAlphabets
for i in test_miss:
print('{} is missing letters {}'.format(i,missing_letters(i)))
the program is giving appropriate output for any string with a single character, like - "b"
that is: b is missing letters acdefghijklmnopqrstuvwxyz
But for a string with multiple characters, like: "zzz" the output is:
zzz is missing letters aaabbbcccdddeeefffggghhhiiijjjkkklllmmmnnnooopppqqqrrrssstttuuuvvvwwwxxxyyy
But I want:
zzz is missing letters abcdefghijklmnopqrstuvwxy
The set type already handles most of the work for you.
import string
def missing_letters(s):
return ''.join(sorted(set(string.lowercase) - set(s)))
If x and y are sets, then x - y is the set of items present in x but not in y. One you have the set of missing characters, you can sort them and re-join the sorted elements into a single string.
>>> set('abc') - set('a')
set(['c', 'b'])
>>> sorted(_)
['b', 'c']
>>> ''.join(_)
'bc'
Keeping with the spirit of what you have already defined. The issue is that you are testing each letter in the missing_letters function more than once. So you just want to check if the character (from the alphabet) is in the string. So take out the extra for loop which loops through the letters in the argument - like such
alphabet = 'abcdefghijklmnopqrstuvwxyz'
test_miss = ["b","zzz"]
def missing_letters(s):
missingAlphabets = ""
global alphabet
i = 0
while i < len(alphabet):
if alphabet[i] not in s:
missingAlphabets += alphabet[i]
i += 1
sortedmissingAlphabetslists = sorted(missingAlphabets)
sortedmissingAlphabets = ''.join(sortedmissingAlphabetslists)
return sortedmissingAlphabets
for i in test_miss:
print('{} is missing letters {}'.format(i,missing_letters(i)))
b is missing letters acdefghijklmnopqrstuvwxyz
zzz is missing letters abcdefghijklmnopqrstuvwxy
def has_duplicates(t):
t= histogram(t)
for i in t:
a=(t[i])
if a > 1:
return True
else:
return False
for i in test_dups:
f = has_duplicates(i)
if f == True:
print(i +' '+ 'has duplicates')
else:
print(i +' '+ 'has no duplicates')

Counting by letters in Python

Objective: count by letters instead of integers.
Is there a clean way to count-by-letters in Python-2.7? I have a program where I am enumerating some data by letter, and my solution would not be very clear to someone reading my code.
I've been checking through the standard documentation, but I don't see anything built-in.
What I'm looking for:
for count in range('A', 'G'):
print count
output[1]:
'C'
'D'
'E'
'F'
How I would do it:
Solution A: Use a dictionary
letters = {
1:'A'
2:'B'
3:'C'
...
}
for count in range(2, 6):
print letters[count]
Solution B: Use chr() and ord()
for count in range(2, 6):
print chr(ord('A') + count)
Relevance:
I am working on a sunday paper crytogram solver. Part of my algorithm involves classifying words by their letter code. For example,
print letter_code('banana')
output[2]: 'ABCBCB'
import string
alphabet = string.ascii_uppercase
>>> for char in alphabet[2:6]:
... print char
...
C
D
E
F
>>>
Your Solution B could be expressed:
for charcode in range(ord('B'), ord('G')):
print chr(charcode)
But to attack your larger issue, how about:
from string import ascii_lowercase, ascii_uppercase
def letter_code(string):
indexes = [ascii_lowercase.index(letter) for letter in string]
return "".join(ascii_uppercase[indexes.index(number)] for number in indexes)
print letter_code('banana')
Gives you "ABCBCB"
Here is a function that would do what you wish:
import string
def enumerate(first, last):
alphabet = string.ascii_uppercase
start = alphabet.index(first)
while alphabet[start] != last:
print alphabet[start]
start += 1
print last
Another solution I have become fond of for my particular application is:
alphabet = iter('ABCDEFGHIJKLMNOPQRSTUVWXYZ')
print next(alphabet)

How do I check if a string contains ALL letters of the alphabet in python?

I am trying to write a python program that checks if a given string is a pangram - contains all letters of the alphabet.
Therefore, "We promptly judged antique ivory buckles for the next prize" should return True while any string that does not contain every letter of the alphabet at least once should return False.
I believe I should be using RegEx for this one, but I'm not sure how. It should look similar to this:
import sys
import re
input_string_array = sys.stdin.readlines()
input_string = input_string_array[0]
if (re.search('string contains every letter of the alphabet',input_string):
return True
else:
return False
This is not something I'd solve with a regular expression, no. Create a set of the lowercased string and check if it is a superset of the letters of the alphabet:
import string
alphabet = set(string.ascii_lowercase)
def ispangram(input_string):
return set(input_string.lower()) >= alphabet
Only if every letter of the alphabet is in the set created from the input text will it be a superset; by using a superset and not equality, you allow for punctuation, digits and whitespace, in addition to the (ASCII) letters.
Demo:
>>> import string
>>> alphabet = set(string.ascii_lowercase)
>>> input_string = 'We promptly judged antique ivory buckles for the next prize'
>>> set(input_string.lower()) >= alphabet
True
>>> set(input_string[:15].lower()) >= alphabet
False
This is my solution in python:
alphabet = "abcdefghijklmnopqrstuvwxyz"
sentence = input()
sentence = sentence.lower()
missing = ''
for letter in alphabet:
if letter not in sentence:
missing = missing+letter
if (len(missing) != 0):
print("missing", missing)
else:
print("pangram")
You dont need regex. What you want can be done in two lines with good space efficiency.
ms = itertools.chain(range(ord("a"),ord("z")),range(ord("A"),ord("Z")))
flag = all(chr(o) in string for o in ms)
That's it. string is the string you want to check. flag will be either True or False depending on if all chars are in string or not.
A pangram is a function that contains at least each letter of the alphabet.
I have tried in this way:
def pangram():
n = str(input('give me a word to check if it is a pangram:\n'))
n = n.lower()
n = n.replace(' ','')
if not isinstance(n, str):
return n, False
elif set(n) >= set('abcdefghijklmnopqrstuvxywz'):
return n, True
else:
return n, False
The function isinstance(n, str) checks if n is a string. The function set() gives us a set. For example set('penny') returns {'y', 'e', 'p', 'n'}... as you see it is a set without the repeated letters.
I was doing the same exercise today, maybe it's not the best aproach, but I think it's easy to understand.
def ispangram(s):
stringy = ''
flag = True
validLetters = "abcdefghijklmnopqrstuvwxyz"
#transform the given string in simple string with no symbols only letters
for char in s.lower():
if(char in validLetters):
stringy += char
#check if all the letters of the alphabet exist on the string
for char in validLetters:
if(char in stringy):
pass
else:
flag = False
break
return flag
if(ispangram("We promptly judged antique ivory buckles for the next prize")):
print("It's PANGRAM!")
else:
print("It's not Pangram :(")
import string
def ispangram(str1, alphabet=string.ascii_lowercase):
return ''.join(sorted(set(str1.lower().replace(" ","")))) == alphabet
First changed all alphabets to lowercase and then removed all spaces using replace. Then Converted into set to have unique chars and then used sorted function to sort alphabetically. As sorted function gives a list, so join func to join it without spaces and then compared it to all lowercase chars.
Here is my solution:
def isaPangrams(s):
alph = list(string.ascii_lowercase)
s = s.lower()
s = list(s)
for letter in alph:
if letter not in s:
print('not pangram')
present='false'
break
if letter in s:
present = 'true'
if present == 'true':
print('pangram')
if __name__ == '__main__':
s = input()
answer = isaPangrams(s)

python regex matching "ab" or "ba" words

I tried matching words including the letter "ab" or "ba" e.g. "ab"olition, f"ab"rics, pro"ba"ble. I came up with the following regular expression:
r"[Aa](?=[Bb])[Bb]|[Bb](?=[Aa])[Aa]"
But it includes words that start or end with ", (, ), / ....non-alphanumeric characters. How can I erase it? I just want to match words list.
import sys
import re
word=[]
dict={}
f = open('C:/Python27/brown_half.txt', 'rU')
w = open('C:/Python27/brown_halfout.txt', 'w')
data = f.read()
word = data.split() # word is list
f.close()
for num2 in word:
match2 = re.findall("\w*(ab|ba)\w*", num2)
if match2:
dict[num2] = (dict[num2] + 1) if num2 in dict.keys() else 1
for key2 in sorted(dict.iterkeys()):print "%s: %s" % (key2, dict[key2])
print len(dict.keys())
Here, I don't know how to mix it up with "re.compile~~" method that 1st comment said...
To match all the words with ab or ba (case insensitive):
import re
text = 'fabh, obar! (Abtt) yybA, kk'
pattern = re.compile(r"(\w*(ab|ba)\w*)", re.IGNORECASE)
# to print all the matches
for match in pattern.finditer(text):
print match.group(0)
# to print the first match
print pattern.search(text).group(0)
https://regex101.com/r/uH3xM9/1
Regular expressions are not the best tool for the job in this case. They'll complicate stuff way too much for such simple circumstances. You can instead use Python's builtin in operator (works for both Python 2 and 3)...
sentence = "There are no probable situations whereby that may happen, or so it seems since the Abolition."
words = [''.join(filter(lambda x: x.isalpha(), token)) for token in sentence.split()]
for word in words:
word = word.lower()
if 'ab' in word or 'ba' in word:
print('Word "{}" matches pattern!'.format(word))
As you can see, 'ab' in word evaluates to True if the string 'ab' is found as-is (that is, exactly) in word, or False otherwise. For example 'ba' in 'probable' == True and 'ab' in 'Abolition' == False. The second line takes take of dividing the sentence in words and taking out any punctuation character. word = word.lower() makes word lowercase before the comparisons, so that for word = 'Abolition', 'ab' in word == True.
I would do it this way:
Strip your string from unwanted chars using the below two
techniques, your choice:
a - By building a translation dictionary and using translate method:
>>> import string
>>> del_punc = dict.fromkeys(ord(c) for c in string.punctuation)
s = 'abolition, fabrics, probable, test, case, bank;, halfback 1(ablution).'
>>> s = s.translate(del_punc)
>>> print(s)
'abolition fabrics probable test case bank halfback 1ablution'
b - using re.sub method:
>>> import string
>>> import re
>>> s = 'abolition, fabrics, probable, test, case, bank;, halfback 1(ablution).'
>>> s = re.sub(r'[%s]'%string.punctuation, '', s)
>>> print(s)
'abolition fabrics probable test case bank halfback 1ablution'
Next will be finding your words containing 'ab' or 'ba':
a - Splitting over whitespaces and finding occurrences of your desired strings, which is the one I recommend to you:
>>> [x for x in s.split() if 'ab' in x.lower() or 'ba' in x.lower()]
['abolition', 'fabrics', 'probable', 'bank', 'halfback', '1ablution']
b -Using re.finditer method:
>>> pat
re.compile('\\b.*?(ab|ba).*?\\b', re.IGNORECASE)
>>> for m in pat.finditer(s):
print(m.group())
abolition
fabrics
probable
test case bank
halfback
1ablution
string = "your string here"
lowercase = string.lower()
if 'ab' in lowercase or 'ba' in lowercase:
print(true)
else:
print(false)
Try this one
[(),/]*([a-z]|(ba|ab))+[(),/]*

Having trouble with whitespace and strings in a function

Prompt
Turn a string into rollercoaster case. The first letter of the sentence is uppercase, the next lowercase, the next uppercase, and so on.
Code
with open('test.txt') as file:
for line in file:
words = line.split()
for word in words:
chars = list(word)
for index, char in enumerate(chars):
if index == 0:
print char.upper(),
elif is_even(index):
print char.upper(),
elif is_odd(index):
print char,
Input
Sunshine makes me happy, on a cloudy day
Output
S u N s H i N e M a K e S M e H a P p Y , O n A C l O u D y D a Y
This is my first attempt at this problem. I can't think of any other way to do this other than to iterate by each letter. When I do this though I'm just treating the entire sentence as a string and spewing out characters.
You can uppercase just every second letter with an extended slice, picking every second letter:
>>> sample = 'Sunshine makes me happy, on a cloudy day'
>>> sample[::2].upper()
'SNHN AE EHPY NACOD A'
>>> sample[1::2].lower()
'usiemksm ap,o luydy'
Now all you need to do is put those together again:
from itertools import izip_longest
result = ''.join([l
for pair in izip_longest(sample[::2].upper(), sample[1::2].lower(), fillvalue='')
for l in pair])
izip_longest() pairs up the uppercased and lowercased strings again, making sure that if there is an odd number of characters to pad out the series with an empty string.
Demo:
>>> from itertools import izip_longest
>>> ''.join([l
... for pair in izip_longest(sample[::2].upper(), sample[1::2].lower(), fillvalue='')
... for l in pair])
'SuNsHiNe mAkEs mE HaPpY, oN A ClOuDy dAy'
Note that whitespace isn't ignored here; the m of make is lowercased even though the e at the end of Sunshine is too.
If you need to vary the letters more precisely, you can make use of iteration still:
from itertools import cycle
from operator import methodcaller
methods = cycle((methodcaller('upper'), methodcaller('lower')))
result = ''.join([next(methods)(c) if c.isalpha() else c for c in sample])
Here itertools.cycle() lets us alternate between two operator.methodcaller() objects, which either upper or lowercase the argument passed in. We only advance to the next one (using next()) when the character is a letter.
Demo:
>>> from itertools import cycle
>>> from operator import methodcaller
>>> methods = cycle((methodcaller('upper'), methodcaller('lower')))
>>> ''.join([next(methods)(c) if c.isalpha() else c for c in sample])
'SuNsHiNe MaKeS mE hApPy, On A cLoUdY dAy'
If it's whitespace giving you trouble, you should use isalpha() to test if a character is a letter or not.
with open('test.txt') as file:
for line in file:
newstr = ""
go_to_upper = True
for c in line:
if c.isalpha():
if go_to_upper:
newstr += c.upper()
else:
newstr += c.lower()
go_to_upper = not go_to_upper
else:
newstr += c
print newstr
Input: Sunshine makes me happy, on a cloudy day
Output: SuNsHiNe MaKeS mE hApPy, On A cLoUdY dAy
You'll only flip back and forth (using the go_to_upper boolean) when the character in question is a letter of the alphabet. Otherwise, it's outputted normally. Notice that MaKeS starts with a capital letter, though SuNsHiNe ends with a lowercase letter, even with the space in the way.
Also, instead of printing immediately (which gives you the weird spacing) we're putting our characters in a new list, which we'll print out all at once later.
Try this code :
import re
i = 1
with open('test.txt') as file:
for line in file:
words = line.split()
for word in words:
chars = list(word)
for index, char in enumerate(chars):
if re.compile('[a-zA-Z]').search(char):
i+=1
if i%2 !=0:
print char.upper(),
else :
print char.lower(),

Categories

Resources