cornydude247 / github-slideshow

A robot powered training repository :robot:
https://lab.github.com/githubtraining/introduction-to-github
MIT License
1 stars 0 forks source link

Trying to learn how to code #4

Open cornydude247 opened 3 years ago

cornydude247 commented 3 years ago

defining strings in Python

all of the following are equivalent

my_string = 'Hello' print(my_string)

my_string = "Hello" print(my_string)

my_string = '''Hello''' print(my_string)

triple quotes string can extend multiple lines

my_string = """Hello, welcome to the world of Python""" print(my_string)

cornydude247 commented 3 years ago

Accessing string characters in Python

str = 'programiz' print('str = ', str)

first character

print('str[0] = ', str[0])

last character

print('str[-1] = ', str[-1])

slicing 2nd to 5th character

print('str[1:5] = ', str[1:5])

slicing 6th to 2nd last character

print('str[5:-2] = ', str[5:-2])

my_string = 'programiz' my_string[5] = 'a' ... TypeError: 'str' object does not support item assignment my_string = 'Python' my_string 'Python'

del my_string[1] ... TypeError: 'str' object doesn't support item deletion del my_string my_string ... NameError: name 'my_string' is not defined

print('str1 + str2 = ', str1 + str2)