---
title: "Write a Python program to reverse a given string. with explanation"  
description: "Write a Python program to reverse a given string. with explanation"  
author: "Ravi Vishwakarma"  
published: 2025-09-02  
updated: 2025-09-02  
canonical: https://www.mindstick.com/interview/34365/write-a-python-program-to-reverse-a-given-string-with-explanation  
category: "python"  
tags: ["python-3.4", "python"]  
reading_time: 2 minutes  

---

# Write a Python program to reverse a given string. with explanation

```python
# Function to reverse a string
def reverse_user_string(text):
    return text[::-1]     # [::-1] is Python's slicing technique to reverse a string

# Input from user
input_user_string = input("Enter a string: ") #return a string value

# Reverse the string
reversed_user_string = reverse_user_string(input_user_string ) #return a reversed string

# Print the result
print("Reversed string:", reversed_user_string )
```

### Explanation:

**Function definition**:

```python
def reverse_user_string(text):
    return text[::-1]
```

- In Python, strings support **slicing**.
- `text[start:end:step]` is the syntax.
- `[::-1]` means:

   - Start from the end of the string.
   - Move backwards one step at a time.
   - So, it effectively reverses the string.

**Input from the user**:

```python
input_user_string = input("Enter a string: ")
```

- `input()` takes input as a string from the user.

**Call the function**:

```python
reversed_user_string = reverse_user_string(input_string)
```

- Pass the user’s input string to our function.

**Print the result**:

```python
print("Reversed string:", reversed_user_string )
```

## Output :

```plaintext
Enter a string: Python
Reversed string: nohtyP
```

## Answers

### Answer by Ravi Vishwakarma

```python
# Function to reverse a string
def reverse_user_string(text):
    return text[::-1]     # [::-1] is Python's slicing technique to reverse a string

# Input from user
input_user_string = input("Enter a string: ") #return a string value

# Reverse the string
reversed_user_string = reverse_user_string(input_user_string ) #return a reversed string

# Print the result
print("Reversed string:", reversed_user_string )
```

### Explanation:

**Function definition**:

```python
def reverse_user_string(text):
    return text[::-1]
```

- In Python, strings support **slicing**.
- `text[start:end:step]` is the syntax.
- `[::-1]` means:

   - Start from the end of the string.
   - Move backwards one step at a time.
   - So, it effectively reverses the string.

**Input from the user**:

```python
input_user_string = input("Enter a string: ")
```

- `input()` takes input as a string from the user.

**Call the function**:

```python
reversed_user_string = reverse_user_string(input_string)
```

- Pass the user’s input string to our function.

**Print the result**:

```python
print("Reversed string:", reversed_user_string )
```

## Output :

```plaintext
Enter a string: Python
Reversed string: nohtyP
```


---

Original Source: https://www.mindstick.com/interview/34365/write-a-python-program-to-reverse-a-given-string-with-explanation

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
