0

Hi I just started learning programming and tried some looping with file management. I am not sure how to join the string after looping can someone give me some pointers how can i go about doing it? Thanks!

This is my code

import string

testFile = open("test.txt", "r").readlines()

for line in inFile:
    for word in line.split():
        line = word.capitalize()
        print line
3
Contributors
2
Replies
3
Views
5 Years
Discussion Span
Last Post by pyTony
0

It's not obvious what you want to do. If you want to capitalize the first word of the sentence:

test_recs = "The quick brown fox jumped over the lazy dog.  test sentence number two."
new_rec_list=[]
for sentence in test_recs.split(". "):
    if len(sentence):
        new_sentence = sentence.strip().capitalize()
        new_rec_list.append(new_sentence)
print ". ".join(new_rec_list)
1

Be careful with capitalize method:

>>> 'notice: Tony Veijalainen posts to DaniWeb'.capitalize()
'Notice: tony veijalainen posts to daniweb'
>>> 'notice: Tony Veijalainen posts to DaniWeb'.title()
'Notice: Tony Veijalainen Posts To Daniweb'

Edited by pyTony: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.