Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8892829
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T23:06:18+00:00 2026-06-14T23:06:18+00:00

Possible Duplicate: Why empty string is on every string? I wonder why Python returns

  • 0

Possible Duplicate:
Why empty string is on every string?

I wonder why Python returns True whenever I check if the empty string is in a string, and why its index is zero.

For instance:

  • '' in '' => true
  • ''.index('') => 0
  • '' in 'notEmpty' => true
  • 'notEmpty'.index('') => 0

I noticed it when I was writing a ROT13 function, and testing it I found that when I call it on an empty string, it returns 'n' ('n' is index13 in the alphabet).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T23:06:19+00:00Added an answer on June 14, 2026 at 11:06 pm

    A string S is a substring of the string T if and only if there exist an index i such that T[i:i+len(S)] == S. When S is the empty string you have T[i:i] = '' = S so all the results are correct.

    Also note that T.index('') returns 0 because index returns the first index in which the substring appears, and T[0:0] = '' so that’s definitely the correct result.

    In summary, the empty string is a substring of every string, and all those results are a direct consequence of this.

    Also note that this is peculiar to strings, because strings are sequences of characters, which are themselves strings of length one. For other kind of sequences(such as lists or tuples) you do not get the same results:

    >>> (1,2,3).index(())
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ValueError: tuple.index(x): x not in tuple
    >>> [1,2,3].index([1,2])
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ValueError: [1, 2] is not in list
    >>> [] in [1,2,3]
    False
    

    That’s because list and tuple only check for members, and not for sub-lists or sub-tuples, because their elements can be of arbitrary type.
    Imagine the case ((1,2),1,2).index((1,2)). Should index check for “sub-tuples”(and thus return 1), for members(and thus return 0) or do some ugly mixture(e.g. first check for sub-tuples and then for members)? In python it was decided to search for members only, since it is simpler and it’s usually what you want. Checking for sub-tuples only would give really odd results in the general case and doing “mixtures” would often yield unpredictable results.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How to check if text is “empty” (spaces, tabs, newlines) in Python?
Possible Duplicate: Empty check with string split I split the below string with '&'
Possible Duplicate: What is the difference between String.Empty and “” Is equivalent to String.Empty
Possible Duplicate: Check if a variable is empty Simple PHP question: I have this
Possible Duplicate: What is the difference between String.Empty and “” Which of the following
Possible Duplicate: What is the difference between String.Empty and “” What is the difference
Possible Duplicate: What is the difference between String.Empty and “” How is String.Empty different
Possible Duplicate: In C#, should I use string.Empty or String.Empty or “” ? Is
Possible Duplicate: Why does Enumerable.All return true for an empty sequence? Code: var line
Possible Duplicate: In C#, should I use string.Empty or String.Empty or “” ? any

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.