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

Recommended Answers

All 2 Replies

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)

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'
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.