Category: 4. Python Strings

  • Escape Characters in Python Explained

    Escape characters in Python are characters that are commonly used to complete specific jobs, and their use in code instructs the compiler to perform the appropriate action for that character. Example: ‘\n’  –>  Leaves a line ‘\t’  –>  Leaves a space  However, in some circumstances, it is preferable not to resolve escapes, in which case…

  • Escape Sequence

    You can’t use both single and double quotations with print() function. For example, if you use “What’s there?”, it will result in a SyntaxError.  >>> print(“He said, “What’s there?””) … SyntaxError: invalid syntax >>> print(‘He said, “What’s there?”‘) … SyntaxError: invalid syntax To tackle this error, you can use triple quotations or escape sequences. A…

  • How to Delete or Change a String in Python?

    You can’t edit or modify strings as they are unchangeable. What this implies is that once you have allocated the elements to a string, you can’t make changes to them. You can only reassign the same name to different strings.  Example ‘Python’ A character in a string cannot be erased or removed. On the other…

  • What is String Slicing in Python?

    A range of characters can be returned using the slice syntax. Specify the start and end indexes, separated by a colon, to retrieve a part of the text. Example: Output llo Slice From the Start The range will begin with the first character if the start index is omitted: Example: Output Hello Slice To the…

  • How to Concatenate Strings in Python?

    When you concatenate two or more strings into one, this process is called concatenation.  The + operator in Python does this. It’s as easy as writing two-string literals together to concatenate them. A string can be made to repeat for a defined number of times by using the * operator. Example:  Output:  str1 + str2 =  HelloWorld! str1 *…

  • What are Strings in Python?

    In Python, strings are byte arrays that represent Unicode characters. Because of the lack of a character data type in Python, a single character is just a one-length string. To access the string’s components, use square brackets. How to Access Characters or Elements From a String in Python? Using indexing, you can access specific characters.…