-2

Suppose, I have a string

ContactMap(const core::data::structural::Structure &structure, double cutoff_distance, core::data::structural::selectors::AtomSelector_SP atoms_in_contact=nullptr)

I need to create a string of * (asterisks) of the same length.

I can think of the following two techniques:

  1. calculate the length of the string, and use a for loop to create a string of the same length
  2. take a copy of the string, and replace each character with an * (asterisk)

Do you have any other ideas?

What is the fastest way to do this?

user366312
  • 16,949
  • 65
  • 235
  • 452

2 Answers2

2

Luckily Python allows to multiply a string and an integer:

string = 'abc'
print('*' * len(string))

outputs

***
DeepSpace
  • 78,697
  • 11
  • 109
  • 154
0

You can use this:

string = "ContactMap(const core::data::structural::Structure &structure, double cutoff_distance, core::data::structural::selectors::AtomSelector_SP atoms_in_contact=nullptr)"
print(string)
print("*" * (len(string)))

If you try it, you can see that the string and line of * are exactly the same length.

The Pilot Dude
  • 2,091
  • 2
  • 6
  • 24