Display string multiple times
Python 2.x:
print '-' * 3
Python 3.x:print('-' * 3)
How to print a string multiple times?
for i in range(3):
print "Your text here"
Orfor i in range(3):
print("Your text here")
How to print a character or string in multiple times in c++
To do what you want you can do this:
#include <iostream>
#include <string>
using namespace std;
int main()
{
int x;
char chars = '*';
cin >> x;
for (int i=1; i<=x; i++){
cout << chars;
}
cout << "\n\n";
system("pause");
return 0;
}
What is a pythonic way to print a string multiple times on one line, separated by spaces?
Put the string in a list
print(' '.join(['hello']*3))
'hello hello hello'
Is there an easy way to return a string repeated X number of times?
If you only intend to repeat the same character you can use the string constructor that accepts a char and the number of times to repeat it new String(char c, int count)
.
For example, to repeat a dash five times:
string result = new String('-', 5);
Output: -----
Print character multiple times
You can print in the same line with System.out.print(" _");
so you can use a loop. print
instead of println
does not append a new line character.
for (int i=0; i<5; i++){
System.out.print(" _");
}
Will print: _ _ _ _ _
. Print elements of string multiple times based on position
You can enumerate iterables (lists, strings, ranges, dictkeys, ...) - it provides the index and a value:
text = "abcdef"
for idx,c in enumerate(text):
print(idx,c)
Output:(0, 'a')
(1, 'b')
(2, 'c')
(3, 'd')
(4, 'e')
(5, 'f')
You can use that to print something multiple times. The print command takes 2 optional parameters :print("Bla","blubb", sep=" --->", end=" Kawumm\n")
Output:Bla --->blubb Kawumm
that specify what is printed between outputs and on the end of output - you can specify an end=""
- so you can continue printing on the same line. Doku:
- Enumerate
Edit:
user_string = input()
def accum(s):
t = [] # list to store stuff into
for count, letter in enumerate(s):
total = letter.upper() + letter * (count) # 1st as Upper, rest as is
t.append(total) # add to list
print(*t, sep="-") # the * "unpacks" the list into its parts
accum(user_string)
Unpacking:print( [1,2,3,4,5], sep=" +++ ") # its just 1 value to print, no sep needed
print(*[1,2,3,4,5], sep=" +++ ") # 5 values to print, sep needed
Output:[1, 2, 3, 4, 5]
1 +++ 2 +++ 3 +++ 4 +++ 5
How to print a string multiple times referencing it only once?
On Windows there is a _printf_p() function, which implements the positional parameter.
So instead
printf("col1%1$scol2%1$scol3%1$scol4\n", csv_delimiter);
you should be able to use_printf_p("col1%1$scol2%1$scol3%1$scol4\n", csv_delimiter);
See https://msdn.microsoft.com/en-us/library/bt7tawza.aspx for more info.EDIT:
for Linux/Windows cross compiling use something like this:
#if defined (__WIN32__)
_printf_p(...);
#elif defined (__linux__)
printf(...);
#endif
Related Topics
Pandas - Add New Column to Dataframe from Dictionary
How to Strip All Whitespace from String
Extract Int from String in Pandas
What Does Model.Train() Do in Pytorch
Where Do the Python Unit Tests Go
Rotating a Two-Dimensional Array in Python
How to Frame Two for Loops in List Comprehension Python
How to Ignore One Single Specific Line with Pylint
Python and Operator on Two Boolean Lists - How
How to Convert an Integer to the Shortest Url-Safe String in Python
Dump to JSON Adds Additional Double Quotes and Escaping of Quotes
How to Crop an Image Using Pil
Python - Datetime with Timezone to Epoch
Overriding the Save Method in Django Modelform
How to Update SQLalchemy Row Entry
How to Suppress or Capture the Output of Subprocess.Run()
"Overflowerror: Python Int Too Large to Convert to C Long" on Windows But Not MAC