Lets_recreate_it_with_python, This is my stream for all python related content you may like?

Today, I am going to recreate the everyday “capitalize each word” case, toggle case and sentence case that you see when using Microsoft word, illustrated below;

I am going to write my own code that will have the same function as those used in word

SENTENCE CASE

The sentence case has the function of turning a group highlighted words into a sentence line for example “MY NAME IS EASYPYTHON” will become “My name is easypython.”

Lets draw a draft of the algorithm we will be using:

  • First, to loop over all the words in a line
  • Check if the word is the first or last or in-between word
  • Next, to loop over all the letters in each word
  • Next, to capitalize the first letter of the first word in a line.
  • Add a “.” at the end of the last word
  • Make all other words small case
  • Print out our result

Below is the code on the sentence case look alike, save as sentence_case.py

# SENTENCE CASE

SOMETHING = input("Write your sentence : ") # this will get input that we will process

WORDS = SOMETHING.split()    # this will generate a list for all the words provided in SOMETHING


# this will get us the first and last words provided in SOMETHING
FIRSTWORD = WORDS[0]
LASTWORD = WORDS[len(WORDS)-1]

NEWSENTENCE = []    # this will act as new container for the sentence case


# this will get us the new words for the sentence case
# will loop over all the words in the WORDS container
for word in WORDS:
        # this will check if a looped over word is the first word in WORDS container
	if word == FIRSTWORD:
		new_word = ""     # this will create an empty string to start our new first word
		for letter in word:
                        # to check if it is the first letter
			if letter == word[0]:
				new_word += letter.upper()  # then will add a capital case to the empty string
			# to check if it is not the first letter
                        if letter != word[0]:
				new_word += letter.lower()  # then will add a lower case to the empty string
                NEWSENTENCE.append(new_word)         # add the new words to the NEWSENTENCE container
	
        # check if a looped over word is the last word
	if word == LASTWORD:
		new_word = word.lower() + "."    # will add "." to the end of that word 
		NEWSENTENCE.append(new_word)     # add the new word to NEWSENTENCE container
	
        # check if a looped over word is not the first and not the last word
	if word != FIRSTWORD and word != LASTWORD:
		new_word = word.lower()      # will add the lower case of that word to empty string
		NEWSENTENCE.append(new_word)  # will add the new word to sentence case

# this will provide the sentence case we generated by joining the words from above	
NEWSENTENCE = " ".join(NEWSENTENCE)

# this will create a new line -- Not important
print("\n\n")

# this will display the sentence case
print(NEWSENTENCE)

the comments are those beginning with “#” and hence make the code self explanatory. Now try running these in your command line and type e.g.

  • 1. MY NAME IS EASYPYTHON
  • 2. Automate Daily Stuff With Python
  • 3. hello pythoneers and pythonistas

they should now output as

  • My name is easypython.
  • Automate daily stuff with python.
  • Hello pythoneers and pythonistas.

Ok enough of that lets try our Hands on TOGGLE CASE

TOGGLE CASE

the toggle case allows a user to convert a word or group of words into alternating capital and lower case words. Example, “Python is fun” will become “pYtHoN iS fUn”.

below is the my code of this

# TOGGLE CASE

# this will act as a container for what you type
SOMETHING = input("Type something here : ")

# this will get a list of all the words in SOMETHING
WORDS = SOMETHING.split()

# this will act as a container for all the toggle case
NEWSENTENCE = []

# this will loop over all the words in WORDS
for word in WORDS:
	new_word = "" # an empty string for making a new word
	count = 0     # if count is O a letter will be lowercase and uppercase if 1.
	for letter in word:
		if count == 0:
			new_word += letter.lower()   # will add a lowercase to new_word
			count = 1
		elif count == 1:
			new_word += letter.upper()   # will add an uppercase to new_word
			count = 0
	NEWSENTENCE.append(new_word)  # will add the new word to NEWSENTENCE

NEWSENTENCE = " ".join(NEWSENTENCE) # will join the words in NEWSENTENCE into an actual sentence
print("\n\n")           # not important
print(NEWSENTENCE)      # will print out output

Test out with the examples in the sentence case in command line

Finally, capitalize each word

CAPITALIZE EACH WORD

This is used to set all the words highlighted under it so the first letter is capitalized e.g. “hello python world” will become “Hello Python World” or “YOU ONLY LIVE ONCE” will become “You Only Live Once”.

# CAPITALIZE EACH WORD

SOMETHING = input("Type something here : ") # will act as a container for the words you type
WORDS = SOMETHING.split() # will get a list for the words you type

NEWSENTENCE = []    # will act as container for new words
for word in WORDS:  # will loop over all the words in WORDS
	new_word = ""   # empty word to begin new words
	for letter in word:                  # will loop over all letters in a word
		if letter == word[0]:            # check if is the first word
			new_word += letter.upper()   # add an upper case to the new word
		if letter != word[0]:            # check if is bot the first word
			new_word += letter.lower()   # add a lower case to the new word
	NEWSENTENCE.append(new_word)         # add the new_word to NEWSENTENCE

NEWSENTENCE = " ".join(NEWSENTENCE)      # join the words in NEWSENTENCE into a sentence
print("\n\n")                            # not important- create a new line
print(NEWSENTENCE)                       # print output

test out with a few examples.

Hope that was informative. I have an app that i am definitely trying to recreate and I know you guys will definitely love it.

Ok until next time, …Lets recreate.

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here