String contains

2 snippets across 2 stacks - JavaScript, Python

JSJavaScript

String Searching

JS · Strings
Syntax
str.includes(search, startIndex)
str.startsWith(search)
str.endsWith(search)
Example
const msg = "Order #1234 shipped";
console.log(msg.includes("shipped"));   // true
console.log(msg.startsWith("Order"));   // true
console.log(msg.endsWith("shipped"));   // true
console.log(msg.indexOf("#"));          // 6

Note All search methods are case-sensitive. For case-insensitive checks, lowercase both sides first: str.toLowerCase().includes(term.toLowerCase()).

PYPython

String Content Checks

PY · Strings
Syntax
str.isdigit() / str.isalpha() / str.isalnum()
Example
pin = "4821"
print(pin.isdigit())
print("hello123".isalnum())
print("  ".isspace())
print("api" in "api_key_value")
Output
True
True
True
True

Note The 'in' operator checks for substring presence and is usually more practical than the .is*() family for real-world validation.

Frequently asked questions

How does JavaScript handle string contains?
This task is covered in 2 stacks on this page: JavaScript, Python. The "String Searching" snippet in JavaScript uses `str.includes(search, startIndex)`.
Which code does the JavaScript example use?
The "String Searching" snippet uses `str.includes(search, startIndex)`, from the Strings section of the JavaScript cheat sheet.
Which stacks cover "string contains" on this page?
JavaScript, Python. Together they hold 2 copy-ready snippets for this task.
Is there anything to watch out for?
Yes. For "String Searching": All search methods are case-sensitive. For case-insensitive checks, lowercase both sides first: str.toLowerCase().includes(term.toLowerCase()).