1. Python Casting:
1.1 Specify a Variable Type:
There may be times when you want to specify a type on to a variable. This can be done with casting.
Python is an object-orientated language, and as such it uses classes to define data types, including its primitive types.
Casting in python is therefore done using constructor functions:
- int() - constructs an integer number from an integer literal, a float literal (by rounding down to the previous whole number), or a string literal (providing the string represents a whole number)
- float() - constructs a float number from an integer literal, a float literal or a string literal (providing the string represents a float or an integer)
- str() - constructs a string from a wide variety of data types, including strings, integer literals and float literals
i.Example for Integers:
x = int(1) # x will be 1
y = int(2.8) # y will be 2
z = int("3") # z will be 3
ii.Example for Floats:
x = float(1) # x will be 1.0
y = float(2.8) # y will be 2.8
z = float("3") # z will be 3.0
w = float("4.2") # w will be 4.2
iii.Example for Strings:
x = str("s1") # x will be 's1'
y = str(2) # y will be '2'
z = str(3.0) # z will be '3.0'
2 Python Strings:
2.1 String Literals:
String literals in python are surrounded by either single quotation marks, or double quotation marks.'hello' is the same as "hello". Strings can be output to screen using the print function.For example: print("hello"). Like many other popular programming languages, strings in Python are arrays of bytes representing unicode characters. However, Python does not have a character data type, a single character is simply a string with a length of 1. Square brackets can be used to access elements of the string.
i. Example for Get the character at position 1 (remember that the first character has the position 0):
a = "Hello, World!"
print(a[1]) #output will be e
ii. Example for Substring. Get the characters from position 2 to position 5 (not included):
b = "Hello, World!"
print(b[2:5]) #output will be llo
iii. Example for The strip() method removes any whitespace from the beginning or the end:
a = " Hello, World! "
print(a.strip()) #output will be "Hello, World!"
iv. Example for The len() method returns the length of a string:
a = "Hello, World!"
print(len(a)) #output will be 13
v. Example for The lower() method returns the string in lower case:
a = "HELLO, WORLD!"
print(a.lower()) #output will be hello, word!
vi. Example for The upper() method returns the string in upper case:
a = "Hello, World!"
print(a.upper()) #output will be HELLO, WORLD!
vii. Example for The replace() method replaces a string with another string:
a = "Hello, World!"
print(a.replace("H", "J")) #output will be Jello, World!
viii. Example for The split() method splits the string into substrings if it finds instances of the separator:
a = "Hello, World!"
print(a.split(",")) #output will be ['Hello', ' World!']
2.2 Command-line String Input :
Python allows for command line input.That means we are able to ask the user for input.
The following example asks for the user's name, then, by using the input() method, the program prints the name to the screen:
Example :
demo_string_input.py
print("Enter your name:")
x = input()
print("Hello, " + x)