Skip to content

Latest commit

 

History

History
64 lines (57 loc) · 1.77 KB

File metadata and controls

64 lines (57 loc) · 1.77 KB

Strings in Python

  • Strings in python can be in either double quotes or single quotes
my_str = 'Hello World'
string = "Hello"

String Concatenation

  • We Concatenate strings using + operator
str1 = 'Hello'
str2 = 'World'
print(str1+str2)
#This will print out Hello World
  • You cannot concatenate strings with integers
8 + 'hello'
# This will give an error
but we can do this using an str() function and it will convert the the int into str then you can concatenate
  • You can also use the += operator to concatenate strings
string = "Cat"
string += " Dogs"
print(string)
#This will print out Cat Dogs

Formatting Strings

There's a new method to interpolate the strings

  • The method is called the F-strings this will help you convert the int value into a string
x = 10
print(f"I have told you  {x}  times to clean the floor correctly")
# This will print I have told you 10 times to clean the floor correctly
# This a new way of interpolating string 
y = 'I have told you {x} times to clean the floor correctly'.format(x)
# This an old way of doing it and its applicable in **python 2.7**

String Index

x = "Hello"
print(x[0])
#This will print out H 
# [] We use this for index
# This is useful for lists

Converting DataTypes

  • We use built-in functions to convert the data types.
  • For example int() str() float()
  • int () this is a built-in function that is used to convert a number into an integer.
  • float() This is used to convert the number into a float
  • str() This is used to convert to a string Note: We used input() function to get the user input
x = input('What is your age') #this will prompt to the user
print(x) # whatever user types it will be printed out