Hints & Tips on Learning Python (Strings).

Python Logo

Here are some Python hints/tips that i have come across when learning to program Python and i thought to share them with you.

These small little Python code snippets that will enhance your bigger Python applications as you grow as a Python programmer.

Python Code example 1)...

Take the a string as input and covert it into a list

stringtolist.py

print("Enter a string of data?")

my_list = list(map(int, input().split()))

print(my_list)

Python 3.7.5 (v3.7.5:5c02a39a0b, Oct 14 2019, 18:49:57)

Type "help", "copyright", "credits" or "license()" for more information.

>>>

Enter a string of data?

10 20 30 40 50

[10, 20, 30, 40, 50]

>>>

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Python Code example 2)...

Python string formatting

The following example summarises the ‘f’ string formatting option in Python 3.

Since Python 3.0, the format() function was introduced to provide advance formatting options.

Python f-strings are available since Python 3.6. The string has the f prefix and uses { } to evaluate variables.

format_string.py

name = ‘Andrew’

age = 51

print (f'{name} is {age} years old')

Python 3.7.5 (v3.7.5:5c02a39a0b, Oct 14 2019, 18:49:57)

Type "help", "copyright", "credits" or "license()" for more information.

>>>$ python format_string.py

Andrew is 51 years old

>>>

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Python Code example 3)...