Strings Revisite

sentence = "This is a sentence"

print(sentence[0:4])
print(sentence[:4])

print(sentence.split()) # delimeter - default is a space

sentence_split =sentence.split()
sentence_join = '-'.join(sentence_split) # This will join the sentence from being split and replacing space with - 
print(sentence_join)


ip_add = "192.168.68.1"

print(ip_add[:4])
print(ip_add.split('.')) # delimeter - split in period

ipadd_split = ip_add.split('.') # This will split the ip address from 192.168.68.1 to 192 168 68 1
ipadd_join = '.'.join(ipadd_split) # This will join the ip address from 192 168 68 1 to 192.168.68.1
print(ipadd_join)


quote = "He said, 'give me all your money'"
quote = "He said, \"give me all your money\"" # Escape characters 
print(quote)

too_much_space = "                   hello       " 
print(too_much_space.strip()) # Strip spaces


print("A" in "Apple") #return True
print("a" in "Apple") #return False - case sensitive

letter = "A"
word = "Apple"
print(letter.lower() in word.lower()) #improved

user_input = input("Enter yes or no: ")
if user_input.lower().strip() == "yes": # It will lower case the input (YeS, Yes to yes) then strips spaces 
    print("You agree!")
else:
    print("You disagree!")


movie = "The Hangover"
print("My favorite movie is {}.".format(movie))
print("My favorite movie is %s." % movie)
print(f"My favorite movie is {movie}.")

Last updated