Live AI Powered DevOps with AWS
PythonStrings

More on Strings


Strings are one of the most fundamental data types in Python, widely used for storing and manipulating textual information. Python offers powerful features for string creation, concatenation, indexing, slicing, and transformation. Understanding these concepts is essential for writing clean and effective Python code.

String Creation and Concatenation

Strings in Python can be created using single quotes (' ') or double quotes (" ").

Example

name = "youtube"
print(name)                 # youtube

Python supports two forms of concatenation:

(1) Implicit Concatenation (Side-by-Side Literals)

Placing two string literals next to each other will concatenate them automatically.

'navin' 'telusko''navintelusko'

Implicit concatenation only works for literals, not variables.

name = 'navin'
name 'telusko'   # ❌ SyntaxError

(2) Using the + Operator

This is the standard and most reliable approach, especially when combining variables with strings.

name = "navin"
print(name + " telusko")    # navin telusko

String Indexing

Strings in Python behave like arrays of characters. Each character occupies a specific position known as an index.

Python supports two indexing systems:

(a) Positive Indexing (Left to Right)

Starts from 0, which represents the first character.

Positive Slicing

Example:

text = 'telusko'
text[0]   # 't'
text[3]   # 'u'

Requesting an index beyond the string length results in an error:

text[7]   # IndexError

(b) Negative Indexing (Right to Left)

Starts from -1, representing the last character.

Negative Slicing

Example:

text[-1]   # 'o'
text[-7]   # 't'

Negative indices below the valid range result in an error:

text[-9]  # IndexError

String Slicing

Slicing allows extracting a part of a string using the format:

string[start : end]

  • Start index is included
  • End index is excluded
  • If end is omitted or exceeds length, slicing continues till the end
  • If start is omitted, slicing begins at the start

Examples:

text = 'telusko'

text[3:7]   # 'usko'
text[0:3]   # 'tel'
text[2:9]   # 'lusko'   (end exceeds length → safe)
text[2:]    # 'lusko'
text[:4]    # 'telu'

Special Case – Full Slice:

text[:]     # 'telusko'

Slicing makes strings highly flexible and powerful for data extraction.


Strings Are Immutable

In Python, strings cannot be changed after creation.

This means you cannot replace characters using indexing:

text = 'TELusko'
text[0:3] = 'TEL'    # ❌ TypeError

Any transformation like converting to upper or lower case, returns a new string, leaving the original unchanged.


Built-in Functions for String

1. len()

The len() function returns the total number of characters in a string, including spaces and punctuation.

myname = "Navin Reddy"
print(len(myname))  # 11

This is useful for validations, loops, indexing, and data processing.

2. upper()

Converts all characters in the string to uppercase and returns a new string.

text = "Telusko"
new_text = text.upper()

print(text)      # Original → Telusko
print(new_text)  # Uppercase → TELUSKO

3. lower()

Converts all characters in the string to lowercase.

text = "PYTHON Programming"
lower_text = text.lower()

print(text)        # Original → PYTHON Programming
print(lower_text)  # Lowercase → python programming

Multi-line Strings

Python allows multi-line text using triple quotes (""" """ or ''' ''').

Example:

text = """
My name is Navin
Welcome to Telusko
"""
print(text)

Output:

My name is Navin
Welcome to Telusko

Python stores such text with newline characters:

'\nMy name is Navin\nWelcome to Telusko\n'

This is useful for documentation, paragraphs, and formatted text.


Summary

Python provides a rich set of features to work with strings:

  • Strings can be created with single or double quotes.
  • Concatenation can be done using the + operator or implicit literal joining.
  • Indexing allows accessing characters using positive or negative positions.
  • Slicing supports extracting meaningful substrings with flexible boundaries.
  • Strings are immutable — methods return new values rather than altering originals.
  • Multi-line strings allow clean storage of paragraphs and text blocks.
  • len() provides an easy way to measure string length.

Written By: Muskan Garg

How is this guide?

Last updated on