6

I want to create a "full file name" variable from several other variables, but the string concatenation and string format operations aren't behaving the way I expect.

My code is below:

file_date = str(input("Enter file date: "))

root_folder = "\\\\SERVER\\FOLDER\\"
file_prefix = "sample_file_"
file_extension = ".txt"

print("")
print("Full file name with concatenation: ")
print(root_folder + file_prefix + file_date + file_extension)
print("Full file name with concatenation, without file_extension: ")
print(root_folder + file_prefix + file_date)
print("")

print("")
print("Full file name with string formatting: ")
print("%s%s%s%s" % (root_folder, file_prefix, file_date, file_extension))
print("Full file name with string formatting, without file_extension: ")
print("%s%s%s" % (root_folder, file_prefix, file_date))
print("")

The output when I run the script is:

C:\Temp>python test.py
Enter file date: QT1

Full file name with concatenation:
.txtRVER\FOLDER\sample_file_QT1
Full file name with concatenation, without file_extension:
\\SERVER\FOLDER\sample_file_QT1


Full file name with string formatting:
.txtRVER\FOLDER\sample_file_QT1
Full file name with string formatting, without file_extension:
\\SERVER\FOLDER\sample_file_QT1

I was expecting it to concatenate the ".txt" at the very end, except it's replacing the first four characters of the string with it instead.

How do I concatenate the extension variable to the end of the string instead of having it replace the first n characters of the string?

In addition to how to solve this particular problem, I'd like to know why I ran into it in the first place. What did I do wrong/what Python 3.2 behavior am I not aware of?

2 Answers 2

8

I think the method input used in your example, like so:

file_date = str(input("Enter file date: "))

may be returning a carriage return character at the end.
This causes the cursor to go back to the start of the line when you try to print it out. You may want to trim the return value of input().

Sign up to request clarification or add additional context in comments.

5 Comments

Confirmed. After running the OP's code, repr(file_date) is 'QT1\r'
I had a feeling it was something this simple. It was adding a carriage return "\r" instead of line feed, but you helped steer me in the right direction. Thanks!
In Python 3, input returns a string, so there's no need to use str.
@MRAB: This behaviour appears to be a BUG in Windows Python 3.x; 2.7 raw_input() doesn't leave an unwanted unexpected \r behind. Did you report it?
@John: No, it has already been reported (issue #11272). Anyway, I wasn't commenting on the bug, but on the 'str' which isn't necessary because in Python 3 'input' returns a string.
3

Use this line instead to get rid of the line feed:

file_date = str(input("Enter file date: ")).rstrip()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.