Write a program that inputs a string from the keyboard and determines the length of the string. Print the string using twice the length as the field width
时间: 2024-02-28 11:56:35 浏览: 136
编写一个程序,求字符串的长度
5星 · 资源好评率100%
Sure, here's an example program in Python:
```python
string = input("Enter a string: ")
length = len(string)
print(f"{string:{length*2}}")
```
This program first prompts the user to enter a string using the `input()` function, and then calculates the length of the string using the `len()` function and stores it in the `length` variable.
Finally, it uses an f-string to print the string with a field width of twice the length of the string, which is achieved by multiplying the length by 2 and passing it as the field width in the f-string. The `:` character is used to specify the field width in the f-string.
阅读全文