Uncategorized

20:Strings

Strings

Strings seem like simple variables like integer, float or Boolean but strings are different. In this lecture we will understand why strings have special respect and what are the main operations which can perform on strings.

  1. Strings might be the 70% of data total on the internet.
  2. Strings simply declare as normal variables st = “Hello World!”.
  3. But strings are different.
  4. In fact, strings are a list of characters.
  5. Strings cannot store like a simple variable.
  6. String stored in data memory by referencing while integer, float or bool store as value type.
  7. In Python, strings can be stored with the help of single quotes or double quotes.
str1 = "Hello World!"str2 = 'Hello World!'

Code 1: Two Styles of Creating String Variable

  1. Strings can store in paragraph (multiline style) by using triple quotes
paragraph = """This is a paragraph.It contains multiple lines."""

Code 2: Multiline String

  1. A single element of string can be accessed but the single element will also be a string.
c = str1[6]

Code 3: Getting a Single Character from String

  1. The c will contain W because it is on index 6.
  2. We can get substring(s) by slicing the operator (colon).
substr = str1[1:4]

Code 4: Apply Slicing on String

  1. We already discussed slicing in the Collections lecture.
  2. Strings are immutable. We cannot change the element by indexing.
  3. We can attach other strings or variables with strings using different ways.
  4. We can attach the other variable at the start, middle or at the end of the string.
  5. Concatenation will not change the string itself.
pages = 320about1 = "This book has " + str(pages) + " pages."

Code 5: Converting Integer to String and Concatenating into other String

about1 = f"This book has {pages} pages.

Code 6: Concatenation by Interpolation

  1. We can find the size of a string (number or characters in a string) by using the len () function. This function returns an integer.
str_size = len (about1)

Code 7: Getting the number of Characters in a String

  1. str_size variable will contain 24 after the execution of code.
  2. There are many other functions in string that perform different kinds of operations.
  3. These functions use like str_variable.function()
  4. First the string variable name or value and then required function.
  5. We can define these functions in different categories.
  6. Case Conversion
  7. Searching
  8. Trim and Replacing
  9. Split and Join
  10. Validations
  11. Case Conversion has the following functions. str1 = “Hello, World!”

Searching functions help to perform search related operations.

Trim and Replace function remove characters or white spaces from strings.

Split and join when we are required to divide the string or attach the string with other strings.

Validation functions check if the condition is true about string.

  1. Escape Sequences are special characters which use with \.
  2. These characters perform some tricks of displaying strings.
  3. \n used to show data on new lines.
print ("Hello\nmy\nfriend")

Code 8: Using \n in a String

  1. We can see that \n is used after Hello and my.
  2. When program execution encounters \n, it breaks the string on the next line.

Figure 1: Output of \n

  1. \t will add 8 spaces in the given string.

Figure 2: Adding 8 spaces after “Hello”

  1. \\ use when you want to display or use simple \ in string
  2. Normally, we use single \ but when we want to use this in a Python string, we have to put one extra \ because standalone \ cause syntax error.
  3. Similarly, standalone apostrophe ‘ and double quotes ” are also required to add \ to use in Python strings.

Leave a Reply

Your email address will not be published. Required fields are marked *