Getting User Input with input()

The input() function allows you to prompt the user for input, which is then replaced by the string the user enters during the program’s execution.

print("Hello " + input("Your name: "))

When the user enters ABC as their name, the output will be: Hello ABC

Finding the Length of a String with len()

The len() function returns the length of a string.

print(len("ABC"))

This will output: 3

You can also prompt the user to enter a string and then display its length:

print(len(input("Enter a string and I will tell the length of it: ")))

Accessing Characters by Index

You can access specific characters in a string by their index. In Python, indexing starts at 0.

print("hello"[1])

This will output: e

Finding the Index of an Array Element

To find the index of an element in a list, use the index() method.

a = ['apple', 'banana', 'mango']
print(a.index('banana'))

This will output: 1

Making Large Numbers Human-Readable

Python allows the use of underscores in numeric literals for better readability.

print(1232_3242_432)

This will output: 12323242432

Rounding Numbers

You can round numbers using the round() function.

print(round(9/2))

This will output: 4

To round a number to a specific number of decimal places, pass the desired precision as the second argument.

print(round(8.32342, 2))

This will output: 8.32

Using the format() Function

The format() function provides more control over formatting numbers.

out = round(11/2, 2)
print(out)

This will output: 5.5

To ensure two decimal places are always displayed, use:

out = round(11/2, 2)
out = "{:.2f}".format(out)
print(out)

This will output: 5.50

Simplifying String Formatting with f-Strings

f-Strings provide a concise and readable way to include variables in strings.

name = "abc"
age = "25"
print(f"Your name is: {name} and age: {age}")

This will output: Your name is: abc and age: 25

Clearing the Screen

In Replit and similar environments, you can clear the screen using:

from replit import clear
clear()

Function Parameters and Arguments

In Python, function parameters and arguments allow you to pass data to functions.

def abc(para):
print(para)
abc(23)

In this example, para is the parameter, and 23 is the argument.

ASCII Art

Popular Python Sites

Expand your Python knowledge by exploring these sites:

Index

				
					# You can access specific characters in a string by their index. 
# In Python, indexing starts at 0.
>>> print("hello"[1])
e

# Finding the Index of an Array Element using the index() method.

>>> a = ['apple', 'banana', 'mango']
>>> a.index('banana')
1

>>> str = 'Hello All! I am coming from Mars'
>>> str[3:7]
'lo A'
>>> str[:7]
'Hello A'
>>> str[5:]
' All! I am coming from Mars'

				
			

Tuple Unpacking

				
					>>> tup_unpack = [(6,4),(3,4),(9,3)]
>>> for item in tup_unpack:
...     print(item)
... 
(6, 4)
(3, 4)
(9, 3)

>>> for (a,b) in tup_unpack:
...     print(b)
... 
4
4
3

				
			
Search

Table of Contents

You may also like to read