Python Forum
Thread Rating:
  • 2 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
list manipulation
#1
I'm trying to make a program that takes any sentence and outputs that same sentence with all the first letters of the words being capitalized. There are exceptions: "and" "not" "the" must be all lowercase as well as any two lettered words.

For example, a string that says 'My name is Mary, not Sally' would output:"my Name is Mary, not Sally".

My strategy is to turn any string into a list of the words and manipulate any exceptions, then add them to the list and put it back to a string again. If there is a more efficient way please let me know.

Screenshots(my progress so far):

https://gyazo.com/20c7af2af42a87d492ef12a31678b657 <-----code
https://i.gyazo.com/a67252ce672d110ca260...fbbea3.png <----output in shell
Reply
#2
We want code, not screen shots. Post your code in python tags (and your output in output tags). See the BBCode link in my signature for instructions.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(Oct-03-2017, 03:44 AM)ichabod801 Wrote: We want code, not screen shots. Post your code in python tags (and your output in output tags). See the BBCode link in my signature for instructions.


def T_case():
    ans = input("Type a fun sentence...")
    wordList = ans.split()
    first = [word[0] for word in wordList]
    exceptions = ['And', 'The', 'Not']
    print(first)
    upperCaseWords = [word[0].upper() +word[1:] for word in wordList]
    print(upperCaseWords)
    for items in upperCaseWords:
        for e in exceptions:
            if e == items:
                items = items.lower()
                print(items)
Output:
Type a fun sentence...i love apples but not oranges ['i', 'l', 'a', 'b', 'n', 'o'] ['I', 'Love', 'Apples', 'But', 'Not', 'Oranges'] not >>>
Reply
#4
First, note that there is a capitalize method of strings:

>>> 'spam'.capitalize()
'Spam'
So you don't need to break the word apart, uppercase the first letter, and put it back together.

Second, when you loop through the list of words at the end, items is a copy of the word, not the word in the list. So you either need to get at the word in the list, or build a new list. Enumerate could get you at the word in the list:

numbers = [8, 0, 1]
for num_index, num in enumerate(numbers):
    if num % 2:
        numbers[num_index] = num * 3 + 1
    else:
        numbers[num_index] = num // 2
print(numbers)
Also note that 'in' can be used as an operator to check if something is in a list or other container. You can just use if items in exceptions:.

In terms of efficiency, you are going through the list twice: once to upper case, then once to lower case. I would go through the list once, starting with a second empty list. Check each word as you go as to whether it needs to be capitalized or not, and then append the word with the appropriate capitalization to the second list. Also, the in operator works faster with sets, so you might want to make exceptions a set.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Problem in list manipulation CyKlop 6 2,283 Oct-18-2021, 09:03 AM
Last Post: DeaD_EyE
  How to pass a list by value for manipulation within a process? bRitch022 4 2,709 Jul-09-2020, 07:13 PM
Last Post: bRitch022
  list or dictionary manipulation dtigue 5 96,774 Jul-21-2017, 03:14 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020