Character, integer, string variables manipulation in Python
Today in this post, we will look at various variables used in python and how to deal with them.
We will understand how to deal with integers, characters, and strings in python.
1. Understanding String, integer/float, Boolean variables.
#String variable character_name="Mike" # Float variable character_age="40.4523" # A new string variable phrase="How Old Are You ?" # Boolean variable is_male=True # Printing string variable with text print("Hello " +character_name + " ") print(phrase + "\n") # Printing integer with text print(character_age+" years\n") print(character_name+" great\n")
Output
Hello Mike How Old Are You ? 40.4523 years Mike great
2. Operation on Strings
# Make the string uppercase print(phrase.upper()) # Make the string text lowercase print(phrase.lower()) # Checks is string is upper and returns true/false. print(phrase.isupper()) # Makes the string upper and then performs the check. # It means it will return true for sure. print(phrase.upper().isupper()) # Gets length of the string "phrase" print(len(phrase)) # Individual characters: get first character print(phrase[0]) print(phrase.index("Are")) # Replacing characters print(phrase.replace("Old","New"))
Output
HOW OLD ARE YOU ? how old are you ? False True 17 H 8 How New Are You ?
3. Operation on Integers
### Working with integers and numbers print(-5.45) print(3+5+3.6) print(3*2+7) ### Convert number to string and print my_num1=2.344 print(str(my_num1) + " is an awesome number") ### Various Operations on string like round, absolute value, and power of. print(abs(my_num1)) print(round(my_num1)) print(pow(3,3)) ### Getting Big and small number print(max(5,9)) print(min(5,9))
Output
/Users/saket/PycharmProjects/1helloworld/venv/bin/python /Users/saket/PycharmProjects/1helloworld/3variables.py -5.45 11.6 13 2.344 is an awesome number 2.344 2 27 9 5 Process finished with exit code 0