# Formatting Text/Time in Python - BGE

**URL:** <https://blenderartists.org/t/formatting-text-time-in-python-bge/635920>\
**Category:** Game Engine Support and Discussion\
**Created:** [March 3, 2015, 11:53am UTC](https://blenderartists.org/t/formatting-text-time-in-python-bge/635920 "2015-03-03T11:53:36Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![hreidmarr](https://blenderartists.org/letter_avatar_proxy/v4/letter/h/8baadc/32.png) [@hreidmarr](https://blenderartists.org/u/hreidmarr)\
**Post date:** [March 3, 2015, 11:53am UTC](https://blenderartists.org/t/formatting-text-time-in-python-bge/635920/1 "2015-03-03T11:53:36Z")

</div>

Hey guys,

I’m trying to format text/the time in this specific script, shared by Monster. What I would like to achieve is for numbers _below 10_ to have a “0” in front of them. So that the time looks like _03:08_ instead of _3:8_

```auto
nTime = datetime.datetime.now()
time_curr = "{}.{}_{}.{}".format(nTime.month, nTime.day, nTime.hour, nTime.minute)
print(time_curr)

```

My question is - how would one do this the most efficiently? I could possibly throw in a few if statements but isn’t there a way to tell Blender to display time/date with 2 digit numbers?

**ALSO:**

This is another question, if anyone knows - how to get a portion of a string?

I know how to get specific characters but how would you do - print the last 8 characters of a string and nothing before them?

Thank you in advance guys, much much appreciated,

Pete

---

<div class="post-metadata">

**Author:** ![VegetableJuiceF](https://blenderartists.org/user_avatar/blenderartists.org/vegetablejuicef/32/22428_2.png) [@VegetableJuiceF](https://blenderartists.org/u/VegetableJuiceF)\
**Post date:** [March 3, 2015, 12:00pm UTC](https://blenderartists.org/t/formatting-text-time-in-python-bge/635920/2 "2015-03-03T12:00:32Z")

</div>

Slicing.

“question”[:4] is ques  
“question”[2:4] is es  
(“0000”+str(3))[-2:] is “03”

> **[Cutting and slicing strings in Python - Python Central](https://www.pythoncentral.io/cutting-and-slicing-strings-in-python/)**
>
> An overview on all of the ways you can cut and slice strings with the Python programming language. With lots of examples/code samples!

---

<div class="post-metadata">

**Author:** ![battery](https://blenderartists.org/letter_avatar_proxy/v4/letter/b/d26b3c/32.png) [@battery](https://blenderartists.org/u/battery)\
**Post date:** [March 3, 2015, 12:01pm UTC](https://blenderartists.org/t/formatting-text-time-in-python-bge/635920/3 "2015-03-03T12:01:26Z")

</div>

> [@hreidmarr"](#):
>
> This is another question, if anyone knows - how to get a portion of a string?
> 
> I know how to get specific characters but how would you do - print the last 8 characters of a string and nothing before them?
> 
> Thank you in advance guys, much much appreciated,
> 
> Pete

Strings can be indexed and sliced like lists:

```auto

myString = "abcdefghijk..."
print(mystring[:8])

```

As for your first question, Im about to leave the house so don’t have time to figure it out. Probably something like:

```auto

if nTime.hour &lt; 10:

timeString = "0" + str(nTime.hour)

```

Then just print/display the constructed string.

Hope this helps!

---

<div class="post-metadata">

**Author:** ![youle](https://blenderartists.org/letter_avatar_proxy/v4/letter/y/898d66/32.png) [@youle](https://blenderartists.org/u/youle)\
**Post date:** [March 3, 2015, 12:05pm UTC](https://blenderartists.org/t/formatting-text-time-in-python-bge/635920/4 "2015-03-03T12:05:51Z")

</div>

oups I come too late:

```auto
import time

print(time.strftime("%b %d %Y %H:%M:%S", time.gmtime()))

chain = "verylongstring"

print(chain[-8:])

```

---

<div class="post-metadata">

**Author:** ![hreidmarr](https://blenderartists.org/letter_avatar_proxy/v4/letter/h/8baadc/32.png) [@hreidmarr](https://blenderartists.org/u/hreidmarr)\
**Post date:** [March 3, 2015, 12:12pm UTC](https://blenderartists.org/t/formatting-text-time-in-python-bge/635920/5 "2015-03-03T12:12:08Z")

</div>

VegetableJuiceF @ NICE! So just needed the [-8:] in the end 🙂

battery @ Yeah I was thinking that aswell with the “if” statement, was just wondering if there was a way to tell blender to format the time differently in the format line…

Anyways, great resources on the slicing, thanks guys! It will be so much easier working with paths now.

Still, If anyone knows about the time formatting thing - please do leave a comment!

[EDIT]

youle @ BIG BIG SHOUT!

Works even better than the way I did it first. Wow this is gonna be awesome, thanks a lot guys

Pete

---

<div class="post-metadata">

**Author:** ![Monster](https://blenderartists.org/user_avatar/blenderartists.org/monster/32/2958_2.png) [@Monster](https://blenderartists.org/u/Monster)\
**Post date:** [March 3, 2015, 12:17pm UTC](https://blenderartists.org/t/formatting-text-time-in-python-bge/635920/6 "2015-03-03T12:17:42Z")

</div>

Try this formatting:

```auto

"{:02d}".format(integerValue)

```

Added to your code:

```auto

...
time_curr = "{:02d}.{:02d}_{:02d}.{:02d}".format(nTime.month, nTime.day, nTime.hour, nTime.minute)
...

```

---

<div class="post-metadata">

**Author:** ![hreidmarr](https://blenderartists.org/letter_avatar_proxy/v4/letter/h/8baadc/32.png) [@hreidmarr](https://blenderartists.org/u/hreidmarr)\
**Post date:** [March 3, 2015, 12:29pm UTC](https://blenderartists.org/t/formatting-text-time-in-python-bge/635920/7 "2015-03-03T12:29:04Z")

</div>

Sweeet, nice one Monster! Works like a beast now 🙂

Trying to set up a multiple-saves system, it’s coming along nicely.

Thanks guys! I owe you all a beer now…

[SOLVED]

---

<div class="post-metadata">

**Author:** ![hreidmarr](https://blenderartists.org/letter_avatar_proxy/v4/letter/h/8baadc/32.png) [@hreidmarr](https://blenderartists.org/u/hreidmarr)\
**Post date:** [March 3, 2015, 1:48pm UTC](https://blenderartists.org/t/formatting-text-time-in-python-bge/635920/8 "2015-03-03T13:48:12Z")

</div>

Oh one more thing while this thread is still active, any way to sort a list backwards? So instead of 1 2 3 4 it’ll be 4 3 2 1.

Or, is there a way a for loop can loop through a list, but backwards, starting from the last entry?

---

<div class="post-metadata">

**Author:** ![battery](https://blenderartists.org/letter_avatar_proxy/v4/letter/b/d26b3c/32.png) [@battery](https://blenderartists.org/u/battery)\
**Post date:** [March 3, 2015, 2:07pm UTC](https://blenderartists.org/t/formatting-text-time-in-python-bge/635920/9 "2015-03-03T14:07:07Z")

</div>

This should do the trick:

```auto

for i in reversed(myList):
print(i)

```

---

<div class="post-metadata">

**Author:** ![hreidmarr](https://blenderartists.org/letter_avatar_proxy/v4/letter/h/8baadc/32.png) [@hreidmarr](https://blenderartists.org/u/hreidmarr)\
**Post date:** [March 3, 2015, 2:12pm UTC](https://blenderartists.org/t/formatting-text-time-in-python-bge/635920/10 "2015-03-03T14:12:27Z")

</div>

Perfect! Thank you very much for all the info
