KnowledgeBoat Logo
|

Robotics & Artificial Intelligence

Explain any four string operators.

Python String Manipulation

1 Like

Answer

The following are some string operators:

1. Replication operator (*) — The replication operator in Python is used to repeat a string for a specified number of times.

Example:

s1 = "Hello"
print(s1 * 4)

Output: HelloHelloHelloHello

2. Concatenation operator (+) — The concatenation operator (+) is used for joining multiple strings.

Example:

print("Hello" + " Hi")

Output: Hello Hi

3. Membership operator (in) — The membership operator returns true if a character exists in the given string.

Example:

if 't' in "Mountain":
    print("t is present in mountain")

Output: t is present in mountain

4. Membership operator (not in) — This operator returns true if a character does not exist in the given string.

Example:

if 'k' not in "Mountain":
    print("k is not present in mountain")

Output: k is not present in mountain

Answered By

2 Likes


Related Questions