Search

Advanced Strings

delimeter:

sentence = "This is a sentence."
print(sentence[:4])
print(sentence.split()) #delimeter - default is a space

splitting and joining (with space):

sentenceSplit = sentence.split()
sentenceJoin = ' '.join(sentenceSplit)
print(sentenceJoin)

adding quote inside string:

quote = 'He said, "give me all your money"'
print(quote)

quote = "He said, \"give me all your money\"" #escapes 1 character after the \
print(quote)

Stripping text:

tooMuchSpace = "             Hello        "
print(tooMuchSpace.strip())

Finding in string:

print("A" in "Apple") #True
print("a" in "Apple") #False

CASE SENSITIVE!

Deal with it:

print(letter.lower() in word.lower())

lower both!

Format

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

f string (3rd example) is the latest and cool kids now