Basic String Operations is a tutorial that covers the fundamental string operations in Python. The tutorial begins by defining what strings are and why they are useful in programming. It explains how strings are used to represent text data in Python, and how to create and manipulate strings using basic string operations.
# String Length
astring = "Hello world!"
print("single quotes are ' '")
print(len(astring))
#Index location
astring = "Hello world!"
print(astring.index("o"))
#Index Counting
astring = "Hello world!"
print(astring.count("l"))
The tutorial covers a wide range of string operations, including indexing and slicing, concatenation and repetition, and conversion to uppercase and lowercase. It also covers string length, checking for substrings, and replacing substrings with other values.
# Printing an array
astring = "Hello world!"
print(astring[3:7])
#Start, Stop, Step
astring = "Hello world!"
print(astring[3:7:2])
#Will print the same
astring = "Hello world!"
print(astring[3:7])
print(astring[3:7:1])
#Reverse String
astring = "Hello world!"
print(astring[::-1])
Throughout the tutorial, there are numerous practical examples and exercises to help reinforce the concepts covered. These examples cover a range of real-world scenarios, such as parsing strings from user input, working with filenames, and extracting data from text files.
# Upper and Lower case
astring = "Hello world!"
print(astring.upper())
print(astring.lower())
#True or False statements
astring = "Hello world!"
print(astring.startswith("Hello"))
print(astring.endswith("asdfasdfasdf"))
astring = "Hello world!"
afewwords = astring.split(" ")
print(astring)
The tutorial concludes by introducing some advanced topics related to working with strings, including regular expressions, which allow you to search for and manipulate strings using complex patterns, and string formatting, which allows you to control the appearance of strings in output.
Basic String Operations Exercise Solution
#Code Completed
s = "Strings are awesome!"
# Length should be 20
print("Length of s = %d" % len(s))
# First occurrence of "a" should be at index 8
print("The first occurrence of the letter a = %d" % s.index("a"))
# Number of a's should be 2
print("a occurs %d times" % s.count("a"))
# Slicing the string into bits
print("The first five characters are '%s'" % s[:5]) # Start to 5
print("The next five characters are '%s'" % s[5:10]) # 5 to 10
print("The thirteenth character is '%s'" % s[12]) # Just number 12
print("The characters with odd index are '%s'" %s[1::2]) #(0-based indexing)
print("The last five characters are '%s'" % s[-5:]) # 5th-from-last to end
# Convert everything to uppercase
print("String in uppercase: %s" % s.upper())
# Convert everything to lowercase
print("String in lowercase: %s" % s.lower())
# Check how a string starts
if s.startswith("Str"):
print("String starts with 'Str'. Good!")
# Check how a string ends
if s.endswith("ome!"):
print("String ends with 'ome!'. Good!")
# Split the string into three separate strings,
# each containing only a word
print("Split the words of the string: %s" % s.split(" "))
Overall, Basic String Operations is an excellent resource for anyone looking to learn the fundamental string operations in Python. Its clear and concise explanations, practical examples, and interactive exercises make it an ideal resource for beginners and intermediate programmers alike. Whether you are looking to manipulate text data or perform more complex string operations in Python, this tutorial is an excellent starting point.