I think you have some slicing issues for example lets say your input is 12 0.1 0.1 0.25 line[1:] is going to be 2 0.1 0.1 0.25 because your input is a string. You can use something like:

line = '12 0.1 0.1 0.25'.split(' ') #convert your string to a list

temp = int(line[0]) #get first element and convert to integer to make comparisons easier

if temp < 3:
    print(' '.join(line))
elif temp == 5: 
    line[0] = '4'
    print(' '.join(line))
elif temp == 7:
    line[0] = '5'
    print(' '.join(line))
elif temp > 8:
    line[0] =str(temp - 3)
    print(' '.join(line))

#Output: 

9 0.1 0.1 0.25

Note: It's better to use elif instead of if in your case because if one of your conditions are true it is not going to check rest of the conditions. More info here

Answer from Inputvector on Stack Overflow
🌐
Stack Overflow
stackoverflow.com › questions › 45905074 › replacing-first-character-in-file-python-2-7
Replacing first character in file Python 2.7 - Stack Overflow
August 27, 2017 - Copyimport os hostName = raw_input("IP: ") communicate = open("communicate.txt", "w") while True: response = os.system("ping " + hostName + " -c 1") if response == 0: # Replace first character with '1' else: # Replace first character with '0' I am running the code in a Linux virtual machine through the terminal. ... Once you open the file with "w", it's already overwritten.
🌐
Stack Overflow
stackoverflow.com › questions › 67270533 › replace-first-character-of-all-lines-in-text-file-with-python-3
Replace first character of all lines in text file with python 3 - Stack Overflow
April 26, 2021 - replace() does not work "in-place". It returns a new string so you would have to assign it back to item in your example (line) in mine. lines_in = [ "2 0.306250 0.376667 0.085000 0.120000\n", "3 0.416250 0.385833 0.090000 0.118333\n", "4 0.354375 0.509167 0.091250 0.121667\n", "2 0.198125 0.206667 0.091250 0.123333\n" ] for line in lines_in: first_char = line[0] if first_char == "2": line = line.replace("2", "0", 1) elif first_char == "3": line = line.replace("3", "1", 1) elif first_char == "4": line = line.replace("4", "2", 1) print(line)
🌐
EyeHunts
tutorial.eyehunts.com › home › python replace the first character in string | example code
Python replace the first character in string | Example code
June 8, 2023 - How to change characters in string Python. Use the list() and join the function. s = "Hello World!" new = list(s) new[0] = 'Y' print(''.join(new)) Output: Yello World! Or you can utilize string slicing and concatenation.
Find elsewhere
🌐
Ispycode
ispycode.com › Blog › python › 2016-08 › Replace-first-occurrence-of-a-char-in-a-string
Replace first occurrence of a char in a string | python blog
Here is a python example that replaces the first occurrence of a character in a string: str1 = "abc abc abc" print(str1) str2 = str1.replace("a", "A" , 1) print(str2) ... Add Seconds To A Datetime Adding Days Armstrong Number Babbage problem Calculate The Average Of A Series Center align string with format Compute and check sha1 message digest Convert Unix Timestamp String To Readable Date Converting seconds to hours minutes and seconds Create A Common Funny File ...
🌐
thisPointer
thispointer.com › home › python › replace first character of a string in python
Replace First Character of a String in Python - thisPointer
April 16, 2022 - It replaced the string’s first character with the character ‘X’. In Python, the string class provides a function replace(substring, replacement, count) to change the contents of string.
🌐
Python Forum
python-forum.io › thread-21391.html
Replace first character
Really having a hard time figuring this out. What I'm trying to do is every time the user inputs text, it will modify and replace every leading character with a '#'. For example the user types: Hello my name is bob, python would print '#ello #y #ame ...
🌐
ItSolutionstuff
itsolutionstuff.com › post › python-replace-first-character-occurrence-in-string-exampleexample.html
Python Replace First Character Occurrence in String Example - ItSolutionstuff.com
October 30, 2023 - In this example, i will add myString variable with hello string. Then we will use replace() function to replace first character occurrence from string in python.
🌐
ItSolutionstuff
itsolutionstuff.com › post › python-replace-first-occurrence-in-string-exampleexample.html
Python Replace First Occurrence in String Example - ItSolutionstuff.com
October 30, 2023 - In this examples, i will add myString variable with hello string. Then we will use replace() function to replace first occurrence from string in python.