Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6744535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:05:44+00:00 2026-05-26T12:05:44+00:00

I love the way Python can format a string with a dictionary: print %(key1)s

  • 0

I love the way Python can format a string with a dictionary:

print "%(key1)s and %(key2)s" % aDictObj

I want to achieve the same thing in Perl with hashes. Is there any snippet or small library to do so?

EDIT:

Thanks for trying this answer. As for me, I came out with a short piece of code:

sub dict_replace
{
    my ($tempStr, $tempHash) = @_;
    my $key;

    foreach $key (sort keys %$tempHash) {
        my $tmpTmp = $tempHash->{$key};
        $tempStr =~ s/%\($key\)s/$tmpTmp/g;
    }

    return $tempStr;
}

It just works. This is not as feature-complete as Python string-formatting with a dictionary but I’d like to improve on that.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T12:05:45+00:00Added an answer on May 26, 2026 at 12:05 pm

    From the python documentation:

    The effect is similar to the using sprintf() in the C language.

    So this is a solution using printf or sprintf

    my @ordered_list_of_values_to_print = qw(key1 key2);
    printf '%s and %s', @ordered_list_of_values_to_print;
    

    There is also a new module which does it using named parameters:

    use Text::sprintfn;
    my %dict = (key1 => 'foo', key2 => 'bar');
    printfn '%(key1)s and %(key2)s', \%dict;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I love the way I can profile a Java/.Net app to find performance bottlenecks
I am in love with Netbeans Source->Format feature. Is there any way to get
One thing I really love about languages such as Python is that if you
Python has this wonderful way of handling string substitutions using dictionaries: >>> 'The %(site)s
One thing I love about Python and PHP is the ability to make a
I found this thread: Best way to strip punctuation from a string in Python
I really love the way Ajax makes a web app perform more like a
I'm using FlexBuilder 3. I love the way the layout guides tell you when
What would be the best way to bring people back to their first love,
What is an elegant way to sort objects in PHP? I would love to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.