Understanding inputStr() from PyInputPlus with Examples

Deep Dive into inputStr() with Examples and Outputs

This post explores inputStr() from PyInputPlus with basic-to-moderate examples.


Why Use inputStr()?

Instead of raw input() plus manual checks, inputStr() handles non-empty validation automatically.


import pyinputplus as pyip

a = pyip.inputStr("Enter Value a-: ")
b = pyip.inputStr("Enter Value b-: ")
c = pyip.inputStr("Enter Value c-: ")

print(f"Value of a :- {a} and Type of a :- {type(a)}")
print(f"Value of b :- {b} and Type of b :- {type(b)}")
print(f"Value of c :- {c} and Type of c :- {type(c)}")
  

Sample Interaction & Output:


Enter Value a-: Python
Enter Value b-: 1
Enter Value c-: @#$
Value of a :- Python and Type of a :- <class 'str'>
Value of b :- 1 and Type of b :- <class 'str'>
Value of c :- @#$ and Type of c :- <class 'str'>
  

Allow Blank Input with blank=True

import pyinputplus as pyip

nickname = pyip.inputStr("Enter nickname (optional): ", blank=True)
print(f"Nickname received: '{nickname}'")
print(len(nickname))

Sample Interaction & Output:

Enter nickname (optional):
Nickname received: 
0

Limit Attempts with limit and default

This example enforces non-empty input but allows only two attempts; after two empty entries, default value is returned.

import pyinputplus as pyip

prompt = "Enter tag (non-empty, 2 attempts): "
tag = pyip.inputStr(prompt,limit=2, default="N/A")
print(f"Tag: '{tag}'")

Sample Interaction & Output:

Enter tag (non-empty, 2 attempts):
(blank entry)
Enter tag (non-empty, 2 attempts):
(blank entry)
Tag: 'N/A'

Or if a non-empty value entered within attempts:

Enter tag (non-empty, 2 attempts): alpha
Tag: 'alpha'

Timeout with timeout and default

Waits for input up to specified seconds; if no entry, returns default.

import pyinputplus as pyip

response = pyip.inputStr(
    "Enter something within 5 seconds: ",
    timeout=5, default="Timed out"
)
print(f"Response: '{response}'")

Sample Interaction & Output:

Enter something within 5 seconds: 
(no input for 5 seconds)
Response: 'Timed out'

If input occurs before timeout:

Enter something within 5 seconds: quick text
Response: 'quick text'

Combine blank=True, limit, timeout, and default

Blank is allowed immediately; otherwise waits up to timeout or attempts, then returns default.

import pyinputplus as pyip

note = pyip.inputStr(
    "Enter note (optional, or wait 3s, up to 2 tries): ",
    blank=True, limit=2, timeout=3, default="[no note]"
)
print(f"Note: {note}")

Sample Interaction & Output (blank first):

Enter note (optional, or wait 3s, up to 2 tries):
Note: 

Sample Interaction & Output (timeout first):

Enter note (optional, or wait 3s, up to 2 tries): 
(no input for 3 seconds)
Enter note (optional, or wait 3s, up to 2 tries): 
(no input again)
Note: '[no note]'

Sample Interaction & Output (valid entry):

Enter note (optional, or wait 3s, up to 2 tries): Reminder
Note: 'Reminder'

Custom Prompt and default Usage

import pyinputplus as pyip

prompt="Type value or wait 4s (default='xyz'): "
value = pyip.inputStr(
    prompt,
    timeout=4, default="xyz"
)
print(f"Final: '{value}'")

Sample Interaction & Output:

Type value or wait 4s (default='xyz'): 
(no input for 4 seconds)
Final: 'xyz'

Preserve Whitespace (strip=False)

If you need to keep leading/trailing spaces:


import pyinputplus as pyip

raw = pyip.inputStr("Enter raw text: ", strip=False)
print(f"Received (with spaces shown): '{raw}' (length {len(raw)})")
  

Sample Interaction & Output:


Enter raw text:   hello world  
Received (with spaces shown): '  hello world  ' (length 15)
  

Happy coding!

📚 Related Topics from PyInputPlus:


Previous Post Next Post

Contact Form