Adding a character to a python str

Asked

Viewed 91 times

0

I have a date of birth where I need the program to read the information and enter the day, month and year using pyautogui. For the WRITE function to work, I need to transform the date, which is an integer, into an str. The place where I need to enter these dates, have 2 digits: 01, 02, 03... 14... 30 and 31. When the str has 2 digits (from 10 below) the program runs smoothly and type the dozens without problems, but when the date becomes with 1 digit, it prints only one house and can not type the 06, for example and typing only 6 not the.

> > import datetime    
    import pyautogui
> > 
> > data_atual = datetime.datetime.now() 
    day = data_atual.day day =
> > str(day)
> > 
> > pyautogui.write(day) # nesse caso imprime 24 pq hj é 24 mas em dias 1,2,3...9 eu tenho problemas.

The pyautogui does not print integer numbers, so there is a need to transform the integer number into one str, but complicates it into 1-digit numbers only. One solution I found to fix this was to include an IF for every result from 1 to 9:

> if day == '1':
>     pyautogui.write('01')  
elif day == '2':
>     pyautogui.write('02')
 .
 .
 . 
elif day == '9':
>     pyautogui.write('09') else:
>     pyautogui.write(day)

I would like something simpler to insert a 0 in Sting day when it had only 1 digit without having to use as many command lines as in if. The insertion of 0 must come forward to transform 1, 2, 3... in 01, 02, 03...

Another solution I found was:

if day <= '9':
        day = ('0' + day)
else:
    day = day

2 answers

2

All these if’s really unnecessary. It may be interesting to check the documentation of the datetime module, there is a method that simplifies what you are trying to do... "strftime" returns a string with specified values and format. For the day attribute it always returns string with two numbers.

import datetime    
import pyautogui

data_atual = datetime.datetime.now()
day = data_atual.strftime("%d")
pyautogui.write(day)

2


Use fstring:

import pyautogui
from datetime import datetime  
  
data_atual = datetime.now() 

pyautogui.write(f'{data_atual.day:02}')

fstring is a literal string prefixed with 'f' or 'F'. These strings may contain substitution fields that are key-delimited expressions {}.

Optionally a format specifier can be included after the expression using : to start this specifier.

In the case of the format specifier 02 informs that an integer whose string representation must have at least two characters will be formatted and the missing characters will be filled with 0.

To learn more see Mini format specification language.

It is also possible using the method datetime.strftime() passing the argument "%d" as a formatting code:

import pyautogui
from datetime import datetime   

data_atual = datetime.now() 

pyautogui.write(data_atual.strftime("%d"))

datetime.strftime() converts object to a string according to a given format and code %d requests the day of the month as a decimal number completing, if necessary, with zero on the left.

  • 1

    Speechless... unlocked me again... did not know this documentation...helped a lot! I will start looking at this now and I will take a course!... vlw

Browser other questions tagged

You are not signed in. Login or sign up in order to post.