View on GitHub

python-for-kids

Learning and exercise content on Python for Kids

Day 7 - String in details

String or Text is very important data type in any programming language we will see some of the basic string operations. You can do magic with string - so beyond this there are many more such operations avaiable which you can play around.

To put any instruction or notes in your code use #

# This line is for NOTES or we call it Comment
# The compiler will ignore this line

To change all the letters in a string to CAPITAL letters.

s = "Hello World"
# all upper or capital
x = s.upper()
print(x)

To change all the letters in a string to lower/small letters.

s = "Hello World"
y = s.lower()
print(y)

To interchange CAPITAL to small and small to CAPITAL in a string.

s = "Hello World"
z = s.swapcase()
print(z)

To make title case i.e., each first letter of every words to CAPITAL.

ss = "all the letters are small and we need to make it title case"
print(ss.title())

In Python every string is an array of characters. The count starts from 0 index value. Hence the first letter is called by 0 and second letter is by 1.

s = "Hello World"
print(s[0])

To find how many characters (letters and symbols) including spaces are there use len() function.

print(len(s))

Remove spaces before and after a string use strip() function. This will ingore any space between the words.

s1 = "   Space before and after      "
print(s1)
print(s1.strip())

To replace a character in a string by another charecter use replace() function.

s2 = s.replace("H", "K")
print(s2)

To use multiline string in code

mult = """This is a sample of 
multiple line text which is otherwise hard
to read in single line."""
print(mult)

To convert a whole string into array of characters

s = "Hello, world"
arr = s.split(',')
print(arr)

To find some text within a string

bigstring = "The big string would contain a lot of words and it is hard to find if a single word is available"
x = "rd" in bigstring
print(x)

Two or more strings can be added by + sign. However if you have many strings to add and put them in places you would like it to be used then use format() function.

# placing
name = "Wriju Ghosh"
phone = "123-456-7890"
email = "wriju@contoso.com"

msg = "Call {} at {} or send email to {}."
print(msg.format(name,phone, email)) # Then need to follow seq

# if seq is predecided
msg2 = "Call {2} at {0} or send email at {1}"
print(msg2.format(phone, email, name)) # place them as per the sequence

Any character in a string which is generally used for something else in Python like " can be used as-is by using the escape character \. Notice below string which has " inside used as-is.

s4 = "He said, \"Hello Everyone\""
print(s4)

Watch the video

Video link

Day 7 - Exercise

  1. Practice the above examples.
  2. The more you practice the better you remeber, programming is muscle memory.
  3. Search for official Python documentation and find two new function and try.

Next: Day 8 - boolean (True/False)

Back to Index