Sitemap
Chandasingh

Chandasingh

Tech Lover!

String Data Types in Python Programming Language

6 min readSep 23, 2020

--

Topics Covered herein:

  • String type
  • Read/Convert
  • Indexing strings []
  • Slicing strings [2:4]
  • Looping through strings with for and while
  • Concatenating strings with +
  • String operations
  • String library
  • String Comparisons
  • Searching in strings
  • Replacing text
  • Stripping white space

String Data Type

  • A string is a sequence of characters
  • A string literal uses quotes 'Hello' or "Hello"
  • For strings, + means “concatenate”
  • When a string contains numbers, it is still a string
  • We can convert numbers in a string into a number using int()
Press enter or click to view image in full size
Press enter or click to view image in full size
Press enter or click to view image in full size

Reading and Converting

  • We prefer to read data in using strings and then parse and convert the data as we need
  • This gives us more control over error situations and/or bad user input
  • Raw input numbers must be converted from strings
Press enter or click to view image in full size
Press enter or click to view image in full size

Indexing and Slicing Strings

  • We can get at any single character in a string using an index specified in square brackets
  • The index value must be an integer and starts at zero
  • The index value can be and expression that is computed
Press enter or click to view image in full size

A Character Too Far

  • You will get a python error if you attempt to index beyond the end of a string.
  • So be careful when constructing index values and slices
Press enter or click to view image in full size

Strings Have Length

  • There is a built-in function “len” that gives us the length of a string
Press enter or click to view image in full size

Len Function

  • A function is some stored code that we use. A function takes some input and produces an output.
Press enter or click to view image in full size

Looping Through Strings

  • Using a while statement and an iteration variable, and the len function, we can construct a loop to look at each of the letters in a string individually
Press enter or click to view image in full size
Press enter or click to view image in full size
  • A definite loop using a for statement is much more elegant
  • The iteration variable is completely taken care of by the for loop
Press enter or click to view image in full size
Press enter or click to view image in full size

Looping and Counting

  • This is a simple loop that loops through each letter in a string and counts the number of times the loop encounters the ‘t’ character
Press enter or click to view image in full size

Looking deeper into “in”

  • The iteration variable iterates through the sequence (ordered set)
  • The block (body) of code is executed once for each value in the sequence
  • The iteration variable moves through all of the values in the sequence
Press enter or click to view image in full size

The iteration variable “iterates” through the string and the block (body) of code is executed once for each value in the sequence

Slicing Strings

  • We can also look at any continuous section of a string using a colon operator
  • The second number is one beyond the end of the slice — “up to but not including”
  • If the second number is beyond the end of the string, it stops at the end
Press enter or click to view image in full size
  • If we leave off the first number or the last number of the slice, it is assumed to be the beginning or end of the string respectively
Press enter or click to view image in full size

Concatenating String with ‘+’

  • When the “+” operator is applied to strings, it means “concatenation”
Press enter or click to view image in full size

Using in as a logical Operator

  • The “in” keyword can also be used to check or to see if one string is in another string
  • The “in” expression is a logical expression that returns True or False and can be used in an if statement
Press enter or click to view image in full size

String Comparison

Press enter or click to view image in full size

String Library

  • Python has a number of string functions which are in the string library
  • These functions are already built into every string — we invoke them by appending the function to the string variable
  • These functions do not modify the original string, instead they return a new string that has been altered
Press enter or click to view image in full size
Press enter or click to view image in full size
Press enter or click to view image in full size

Try out:

Get Chandasingh’s stories in your inbox

Join Medium for free to get updates from this writer.

str.center(width[,fillchar])
str.endswith(suffix[,start[,end]])
str.find(sub[,start[,end]])
str.lstrip([chars])

and

str.replace(old,new[, count])
str.lower()
str.rstrip([chars])
str.strip([chars])
str.upper()

Searching a String

  • We use the find() function to search for a substring within another string
  • find() finds the first occurrence of the substring
  • If the substring is not found, find() returns -1
  • Remember that string position starts at zero
Press enter or click to view image in full size

Making everything UPPER CASE

  • You can make a copy of a string in lower case or upper case
  • Often when we are searching for a string using find() — we first convert the string to lower case so we can search a string regardless of case
Press enter or click to view image in full size

Search and Replace

  • The replace() function is like a “search and replace” operation in a word processor
  • It replaces all occurrences of the search string with the replacement string
Press enter or click to view image in full size

Stripping White space

  • Sometimes we want to take a string and remove white space at the beginning and/or end
  • lstrip() and rstrip() remove white space at the left or right
  • strip() removes both beginning and ending white space
Press enter or click to view image in full size

Prefixes

Press enter or click to view image in full size

Parsing and Extracting

Press enter or click to view image in full size

I hope it helps!

If you liked reading this blog, Don’t forget to clap!

Thanks

--

--